php - CakePHP 3.x - Link and save associations with 3 models through one model -
i using cakephp version 3.x.
i need link 3 models (trackers, articles, mentions) through 1 model (trackersarticlesmentions).
here relations definition:
trackerstable.php
$this->belongstomany('mentions', [ 'through' => 'trackersarticlesmentions', 'savestrategy' => 'append', ]); $this->belongstomany('articles', [ 'through' => 'trackersarticlesmentions', 'savestrategy' => 'append', ]); articlestable.php
$this->belongstomany('mentions', [ 'through' => 'trackersarticlesmentions', 'savestrategy' => 'append', ]); $this->belongstomany('trackers', [ 'through' => 'trackersarticlesmentions', 'savestrategy' => 'append', ]); mentionstable.php
$this->belongstomany('articles', [ 'through' => 'trackersarticlesmentions', 'savestrategy' => 'append', ]); $this->belongstomany('trackers', [ 'through' => 'trackersarticlesmentions', 'savestrategy' => 'append', ]); trackersarticlesmentionstable.php
$this->belongsto('trackers', [ 'foreignkey' => 'tracker_id', 'jointype' => 'inner' ]); $this->belongsto('articles', [ 'foreignkey' => 'article_id', 'jointype' => 'inner' ]); $this->belongsto('mentions', [ 'foreignkey' => 'mention_id', 'jointype' => 'inner' ]); so far working when want find data , include (aka contain) them.
unfortunately, when want link them, should able use function link , this:
$this->trackers->link($trackerentity, [$articleentity, $mentionentity]); but doesn't work, function link expects second parameter:
list of entities belonging
targetside of association
i may wrong, function seems made link 2 entities, second parameter of array of same entity...
i found solution creating function in trackersarticlesmentionstable this:
public function createlink(\app\model\entity\tracker $trackerentity, \app\model\entity\article $articleentity, \app\model\entity\mention $mentionentity) { return $this->findorcreate([ 'tracker_id' => $trackerentity->get('id'), 'article_id' => $articleentity->get('id'), 'mention_id' => $mentionentity->get('id'), ]); } but "properly". have suggestion? thank you!
the link() method ment link single association source side entity, multiple (usually distinct) association target side entities, example, link single post multiple tags, done via tags association.
so means via $this->trackers->link() (i assume $this table object, , therefore $this->trackers association object), link single mention or article multiple (distinct) trackers.
the usage of link() have in mind not supported, have go solution came with, eg create , save trackersarticlesmention entity on own - it's fine that.
Comments
Post a Comment