# |
Jun 3rd 2021, 09:26 |
kevin.pfeifer |
the first parameter is YOUR name what you want to call that association |
# |
Jun 3rd 2021, 09:26 |
slackebot |
'property'=>'last_url_file' ]);``` |
# |
Jun 3rd 2021, 09:26 |
etibor |
In DocumentsTable.php i have: ```$this->hasMany('UrlFiles', ['className'=>'UrlFiles', 'joinType' => 'INNER', 'dependent'=>true, 'property'=>'last_url_file' ]); $this->hasMany('LastUrlFile', ['className'=>'UrlFiles', 'joinType' => 'INNER', 'Limit'=>1, 'Sort'=>([ 'UrlFiles.id' => 'DESC', ]), 'dependent'=>true, |
# |
Jun 3rd 2021, 09:26 |
kevin.pfeifer |
so this doesn't work ```$this->hasMany('UrlFiles', [ 'className'=>'UrlFiles', ... $this->hasOne('UrlFiles', [ 'className'=>'LastUrlFiles', .....``` |
# |
Jun 3rd 2021, 09:25 |
kevin.pfeifer |
association names (the first parameter) need to be unique |
# |
Jun 3rd 2021, 09:25 |
etibor |
yes i try to do it with additional association |
# |
Jun 3rd 2021, 09:25 |
etibor |
but in the Documents Model i dont have url_file_id |
# |
Jun 3rd 2021, 09:25 |
kevin.pfeifer |
as already said in support channel |
# |
Jun 3rd 2021, 09:24 |
kevin.pfeifer |
i would guess it would be easier for you if you add another association |
# |
Jun 3rd 2021, 09:24 |
etibor |
in the UrlFiles Model i have the foreign key |
# |
Jun 3rd 2021, 09:24 |
kevin.pfeifer |
as for cakephp |
# |
Jun 3rd 2021, 09:24 |
kevin.pfeifer |
you need the forgein key to connect the 2 tables |
# |
Jun 3rd 2021, 09:23 |
kevin.pfeifer |
from a DB structure point a hasMany and hasOne looks the same |
# |
Jun 3rd 2021, 09:22 |
etibor |
but i feel that if i dont have the foreign_key in the Main Model it will never be accasseble as HasONe |
# |
Jun 3rd 2021, 09:21 |
etibor |
yes exactly thats the case |
# |
Jun 3rd 2021, 09:21 |
kevin.pfeifer |
which should actually be a hasONe |
# |
Jun 3rd 2021, 09:21 |
kevin.pfeifer |
so you got a hasMany |
# |
Jun 3rd 2021, 09:19 |
etibor |
is there any chance to acces like $last_url_file->url_path insted of $last_url_file[0]->url_path |
# |
Jun 3rd 2021, 09:17 |
philo.hamel |
sorry, I'm not sure |
# |
Jun 3rd 2021, 09:10 |
etibor |
i would like to get only a single record |
# |
Jun 3rd 2021, 09:10 |
etibor |
```$this->hasMany('LastUrlFile', ['className'=>'UrlFiles', 'joinType' => 'INNER', 'Limit'=>1, 'Sort'=>([ 'UrlFiles.id' => 'DESC', ]), 'dependent'=>true, 'property'=>'last_url_file' ]);``` it is working but its returns an array insted of a single record |
# |
Jun 3rd 2021, 09:09 |
etibor |
but actually originally it was an HasMany association, so i dont have a foreign_key in the main model |
# |
Jun 3rd 2021, 09:08 |
etibor |
i having issue with: would like to make another association to the same model, the first is a HasMayn while the second is a HasOne |
# |
Jun 3rd 2021, 09:07 |
etibor |
thank you really much |
# |
Jun 3rd 2021, 09:05 |
kevin.pfeifer |
sure |
# |
Jun 3rd 2021, 09:05 |
etibor |
can i ask you a quick question? |
# |
Jun 3rd 2021, 09:05 |
etibor |
hello Kevin |
# |
Jun 3rd 2021, 09:04 |
etibor |
since i dont have in the Main Model the url_file_id |
# |
Jun 3rd 2021, 09:04 |
etibor |
so back to the question, it does not contained when i use HasOne insted of HasMany |
# |
Jun 3rd 2021, 09:02 |
etibor |
moment |
# |
Jun 3rd 2021, 09:01 |
etibor |
``j |
# |
Jun 3rd 2021, 09:01 |
etibor |
`$this->hasMany('LastUrlFile', [ 'className'=>'UrlFiles', 'joinType' => 'INNER', 'Limit'=>0, 'Sort'=>([ 'UrlFiles.id' => 'DESC', ]), 'dependent'=>true, 'property'=>'last_url_file' ]);` |
# |
Jun 3rd 2021, 08:59 |
philo.hamel |
triple ` |
# |
Jun 3rd 2021, 08:58 |
etibor |
thank you @philo.hamel okey, first please tell me how can i insert a whole code block not just one line here |
# |
Jun 3rd 2021, 08:55 |
philo.hamel |
you'll need conditions to figure out which row to fetch for the association |
# |
Jun 3rd 2021, 08:54 |
slackebot |
->setProperty('work_address') ->setConditions(['WorkAddress.label' => 'Work']) ->setDependent(true); } }``` |
# |
Jun 3rd 2021, 08:54 |
philo.hamel |
```class UsersTable extends Table { public function initialize(array $config): void { $this->hasOne('HomeAddress', [ 'className' => 'Addresses' ]) ->setProperty('home_address') ->setConditions(['HomeAddress.label' => 'Home']) ->setDependent(true); $this->hasOne('WorkAddress', [ 'className' => 'Addresses' ]) |
# |
Jun 3rd 2021, 08:54 |
philo.hamel |
@etibor have you seen this example? |
# |
Jun 3rd 2021, 08:26 |
etibor |
or is this not possible without the Documents table have an last_file_id db field?? |
# |
Jun 3rd 2021, 08:23 |
etibor |
so without the [0] index i would like to get |
# |
Jun 3rd 2021, 08:22 |
etibor |
i do this becuase i would like to get the last inserted url_files record and not reach like $last_model[0]->url_path // this is the only working solution now |