Can not get `hasOne` to work


#1

My database has some unusual naming conventions ( not my choice ).
And I can’t seem to get hasOne reference to work ( haven’t even tried hasMany )

        $urlsSearchTerms = new \atk4\data\Model($this->db, ['table'=>'UrlsSearchTerms','id_field'=>'Index','title_field'=>'title']);
        $userUrls =  new \atk4\data\Model($this->db, ['table'=>'UsersUrls','id_field'=>'Index','title_field'=>'Url']);
        $urlsSearchTerms
            ->hasOne('UrlIndex',$userUrls)
            ->addFields(['Url'])
            //            ->addTitle()
            ;
        $urlsSearchTerms->setLimit(5);

        var_dump($urlsSearchTerms->export());

If I run it as is I get atk4\core\Exception: Child element not found.

If I switch addFields for addTitle I get atk4\core\Exception: Element with requested name already exists

What am I doing wrong here? Been breaking my head on this for a while now.


#2

you should specify “their_field” otherwise I think it looks for default id field and not finding it inside ‘uerurls’.

Also it’s probably best if you create dedicated classes for your models.


#3

if you catch the exception, you can dump out it along with extra information, see documentation on ATK Core.


#4

Thanks, is there documentation on using “their_field” because I believe I tried that and it didn’t help.


#5

if you search there are a few mentions. Can you post your ER diagram here, i’ll try to suggest.


#6

I don’t think my employer woud want me to post it here… But I’ll try to get something close


#7

not the entire database of course, just something so we can understand and help. You can create just a test-case.


#8

Ok tried add thier_field and it didn’t help.

        $urlsSearchTerms
            ->hasOne('UrlIndex',
                     [
                         $userUrls,
                         'their_field' => 'Index'
                     ]
            )
            ->addFields(['Url'])

And I still get the same error

atk4\core\Exception: Child element not found

/var/www/vendor/atk4/core/src/ContainerTrait.php:231
/var/www/vendor/atk4/data/src/Persistence_SQL.php:568
/var/www/vendor/atk4/data/src/Model.php:1680
/var/www/vendor/atk4/data/src/Reference_SQL_One.php:46
/var/www/vendor/atk4/data/src/Field_SQL_Expression.php:77
/var/www/vendor/atk4/dsql/src/Expression.php:275
/var/www/vendor/atk4/dsql/src/Query.php:187
/var/www/vendor/atk4/dsql/src/Expression.php:445
/var/www/vendor/atk4/dsql/src/Expression.php:455
/var/www/vendor/atk4/dsql/src/Query.php:1231
/var/www/vendor/atk4/dsql/src/Expression.php:475
/var/www/vendor/atk4/dsql/src/Connection_Dumper.php:34
/var/www/vendor/atk4/dsql/src/Expression.php:597
/var/www/vendor/atk4/dsql/src/Expression.php:621
/var/www/vendor/atk4/data/src/Persistence_SQL.php:815
/var/www/vendor/atk4/data/src/Model.php:1542
/var/www/tests/Feature/App/Prt/ReportGenerator/DatasourceTest.php:54

#9

@romaninsh

Selection_023

This is just part of a bigger diagram ( that is also a big mess )

Thanks for your help.