Tuesday, October 13, 2015

CakePHP 3: How to save model and field name in associated table?

I need a hint.

I have to develop a number of centralized models, which will be in relation to other models. 
Examples of such models are Timelines and Images, which secound have fields: 

id, 
model, // Model name, ex Users
field, // field name, from where upload
foreign_key, // User Id
descripton..


UsersTable example: 

        $this->hasOne('Cover',[
            'className' => 'Images',
            'foreignKey' => 'foreign_key',
            'conditions' => [
                'model' => 'Users',
                'field' => 'cover'
            ]
        ]);

        $this->hasOne('Avatar',[
            'className' => 'Images',
            'foreignKey' => 'foreign_key',
            'conditions' => [
                'model' => Users',
                'field' => 'avatar'
            ]
        ]);
        $this->hasMany('Images',[
            'className' => 'Images',
            'foreignKey' => 'foreign_key',
            'conditions' => [
                'model' => 'Users',
                'field' => 'images'
            ]
        ]);

<?= $this->Form->input('cover.cover',[ 'type' => 'file', 'label' => false]); ?>
<?= $this->Form->input(avatar.avatar',[ 'type' => 'file', 'label' => false]); ?>
<?= $this->Form->input('images[].images',[ 'type' => 'file', 'multiple' => true, 'label' => false]); ?>


How to in beforeMarshal or beforeSave methods on ImagesTable or Behavior retrieve model name from which is uploaded images, example Users, Posts,.., and store in images tables?

Currently I am migrating from 2.x to 3.x, I have a lot to learn.

Thanks.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: