Monday, August 4, 2014

Re: Saving data to Has Many relationship table from single form in CakePHP 3

Hi Jose Lorenzo,
Thank you. I am following the book on following url to do this.
http://book.cakephp.org/3.0/en/orm/table-objects.html
Something is blocking to save data.
My Table structure is as follows.
===================
A Vendor Table
A Vendor manager table with vendor_id and user_id fields
A user table.

My Vendorstable.php file

=============
<?php
// src/Model/Table/VendorsTable.php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
class VendorsTable extends Table {

   
    public function validationDefault(Validator $validator) {
        $validator
            ->notEmpty('company_name','Company Name is required')
            ->notEmpty('postalcode','Zip /Postcode is required')
            ->notEmpty('subscription_package','Couldn\'t find a valid package')
            ->notEmpty('country','Country is required');

        return $validator;
    }
    public function initialize(array $config) {
        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created_on' => 'new',
                    'modified_on' => 'always',
                ]
               
            ]
        ]);
        $this->belongsTo('SubscriptionPackages', [
            'foreignKey' => 'subscription_package'
        ]);
         $this->hasMany('VendorManagers');
         $this->hasMany('Users', [ 'through' => 'VendorManagers']);
   
    }
}
VendorManagersTable.php
======================
<?php
// src/Model/Table/VendorManagersTable.php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
class VendorManagersTable extends Table {

    public function initialize(array $config) {
        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created_on' => 'new',
                    'modified_on' => 'always',
                ]
               
            ]
        ]);
        $this->belongsTo('Vendors');
        $this->belongsTo('Users');
       
   
    }
}
UsersTable.php
===============
<?php
// src/Model/Table/UsersTable.php
namespace App\Model\Table;

use Cake\ORM\Table;
use Cake\Validation\Validator;
class UsersTable extends Table {

   
    public function validationDefault(Validator $validator) {
        $validator
             ->add('username', 'validFormat', [
                    'rule' => 'email',
                    'message' => 'Username must be valid e-mail'
                ])
            ->add('username', [
                    'unique' => ['rule' => 'validateUnique', 'provider' => 'table']
                ])
            ->add('email', 'validFormat', [
                'rule' => 'email',
                'message' => 'E-mail must be valid'
                ])
            ->add('email', [
                    'unique' => ['rule' => 'validateUnique', 'provider' => 'table']
                ])
            ->add('password', [
                'minLength' => [
                    'rule' => ['minLength', 6],
                    'last' => true,
                    'message' => 'Password must have minimum %d characters.'
                ],
                'maxLength' => [
                    'rule' => ['maxLength', 15],
                    'message' => 'Password can have maximum %d characters'
                ]
            ])
            ->notEmpty('role','A valid role is required');

        return $validator;
    }
    public function initialize(array $config) {
        $this->addBehavior('Timestamp', [
            'events' => [
                'Model.beforeSave' => [
                    'created_on' => 'new',
                    'modified_on' => 'always',
                ]
               
            ]
        ]);
    $this->hasMany('VendorManagers');
    }
}

Controller Function to save form
==========================
public function buypackage($id,$payoption='monthly'){
        
       
        $vendors = TableRegistry::get('Vendors');
        $entity = $vendors->newEntity($this->request->data(), [
            'associated' => ['VendorManagers' => ['associated' => ['Users']]
            ]
        ]);
        if ($this->request->is('post')) {
            if ($this->Vendors->save($entity)) {
                 $this->Flash->success(__('The vendor has been saved'));
                 return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('Unable to add the vendor'));
        }
        $this->set('vendor', $vendors);
        $this->set('subscription_package', $id);
        $this->set('subscription_type', $payoption);
    }

Form - buypackage.ctp
===============

<div class="vendors form">
<?php echo $this->Flash->render('auth') ?>
<?php echo $this->Form->create($vendor) ?>
    <fieldset>
        <legend><?php echo __('Company Details') ?></legend>
        <?php echo $this->Form->input('Vendor.name',['label'=>'Vendor Name']) ?>
        <?php echo $this->Form->input('Vendor.logo', ['type' => 'file']);?>
        <?php echo $this->Form->input('Vendor.fax') ?>
        <?php echo $this->Form->input('Vendor.phone') ?>
        <?php echo $this->Form->input('Vendor.website') ?>
        <?php echo $this->Form->input('Vendor.address') ?>
        <?php echo $this->Form->input('Vendor.country') ?>
        <?php echo $this->Form->input('Vendor.city') ?>
        <?php echo $this->Form->input('Vendor.state') ?>
        <?php echo $this->Form->input('Vendor.postalcode',['label'=>'Zip / Post Code']) ?>
        <?php echo $this->Form->hidden('Vendor.subscription_package',['value' => $subscription_package]); ?>
    </fieldset>
     <fieldset>
        <legend><?php echo __('Primary Contact Details') ?></legend>
        <?php echo $this->Form->input('User.email') ?>
        <?php echo $this->Form->input('User.password') ?>
        <?php echo $this->Form->input('User.conf_password',['label'=>'Confirm Password', 'type' => 'password']) ?>
        <?php echo $this->Form->input('User.title') ?>
        <?php echo $this->Form->input('User.first_name',['label'=>'First Name(s)']) ?>
        <?php echo $this->Form->input('User.laste_name') ?>
        <?php echo $this->Form->input('User.job_title') ?>
        <?php echo $this->Form->input('User.phone') ?>
        <?php echo $this->Form->hidden('User.status',['value' => 'P']); ?>
        <?php echo $this->Form->hidden('User.role',['value' => 'vendor']); ?>
       
    </fieldset>
<?php echo $this->Form->button(__('Checkout')); ?>
<?php echo $this->Form->end() ?>
</div>


Would you please help me to find the mistake I did here.

Thank,
Jipson
On Monday, 4 August 2014 15:10:39 UTC+1, José Lorenzo wrote:
Have you tried following the instructions in the book? What is failing?

On Monday, August 4, 2014 3:43:20 PM UTC+2, Jipson Thomas wrote:
Hi All,
Would you please give me some details or samples of saving data from a single form to the tables main and has many relationships. I tries with the normal way by adding a modelname.fieldname on form and the sual newentity of main table but it through false on save(); Please help me to fix this

Regards,
Jipson

--
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: