سؤال

I have 2 db tables : Users & Projects.

Fields of Users table are : id, name, username, password, email, address, phone.

Fields of Projects table are : id, name, description, user_id, deadline, budget.

What I need is that, I'll have 1 form, where there will be all fields from these 2 tables. And, when I submit the form, these fields will be saved in these 2 tables.

For example, my form will be like this :

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('name',array('type'=>'text','div'=>false));
    echo $this->Form->input('username',array('type'=>'text','div'=>false));
    echo $this->Form->input('password',array('type'=>'password','div'=>'false));
    echo $this->Form->input('email',array('type'=>'email','div'=>false));
    echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
    echo $this->Form->input('phone',array('type'=>'tel','div'=>false));

    echo $this->Form->input('name',array('type'=>'text','div'=>false));
    echo $this->Form->input('description',array('type'=>'text','div'=>false));
    echo $this->Form->input('deadline',array('type'=>'date','div'=>false));
    echo $this->Form->input('budget',array('type'=>'num','div'=>false));
    echo $this->Form->submit('Save');
    echo $this->Form->end();
?>

Now, I want that when I submit the form, the fields will be saved in the corresponding tables; Users table will receive & save its fields, and Projects table will receive & save its fields. For this, I tried hasMany association between Users & Projects tables, means, each user from Users table will have many projects from Projects table.

In User.php, I tried this :

class User extends AppModel{
   public $hasMany=array(
              'Project'=>array(
              'className'=>'Project')
   );
}

I thought it'd work, but it didn't, only Users table get its values, no value goes to Project table. What is the problem here ? What should I do ?

Thanks.

هل كانت مفيدة؟

المحلول

try this in your view

echo $this->Form->create('User');
echo $this->Form->input('name',array('type'=>'text','div'=>false));
echo $this->Form->input('username',array('type'=>'text','div'=>false));
echo $this->Form->input('password',array('type'=>'password','div'=>false));
echo $this->Form->input('email',array('type'=>'email','div'=>false));
echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
echo $this->Form->input('phone',array('type'=>'tel','div'=>false));

echo $this->Form->input('Project.0.name',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.description',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.deadline',array('type'=>'date','div'=>false));
echo $this->Form->input('Project.0.budget',array('type'=>'num','div'=>false));
echo $this->Form->submit('Save');
echo $this->Form->end();

And in your controller you have to use

$this->User->saveAssociated($data);

see the manual about how to save associated data

نصائح أخرى

At first, add the model names User and Project as prefix to your form fields to separate them:

View:

<?php
echo $this->Form->create('User');
echo $this->Form->input('User.name',array('type'=>'text','div'=>false));
echo $this->Form->input('User.username',array('type'=>'text','div'=>false));
echo $this->Form->input('User.password',array('type'=>'password','div'=>false));
echo $this->Form->input('User.email',array('type'=>'email','div'=>false));
echo $this->Form->input('User.address',array('type'=>'textarea','div'=>false));
echo $this->Form->input('User.phone',array('type'=>'tel','div'=>false));

echo $this->Form->input('Project.name',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.description',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.deadline',array('type'=>'date','div'=>false));
echo $this->Form->input('Project.budget',array('type'=>'num','div'=>false));
echo $this->Form->submit('Save');
echo $this->Form->end();

At second, use the separation to save your data into different models. Do not forget to add an associated column user_id to your table projects.

Controller

class UserController extends AppController
{
    function create()
    {
        $this->loadModel('Project');

        if($this->request->is('post', 'put'))
        {
            $this->User->getDataSource()->begin($this); // Starts transaction
            if($this->User->save($this->request->data['User']))
            {
                // Success, save project now
                $this->request->data['Project']['user_id'] = $this->User->getLastInsertId();

                if($this->Project->save($this->request->data['Project']))
                {
                    $this->User->getDataSource()->commit($this); // Commit
                    // success
                } else {
                    $this->User->getDataSource()->rollback($this); // Rollback
                }
            } else {
                $this->User->getDataSource()->rollback($this); // Rollback
            }
        }
    }
}

I tested this code → works.

Your question is not very clear to me but what I see is you need to save data using associations.

But an association is a connection between two Active Record models. Associations are created to maintain Primary Key-Foreign Key information between instances of the two models.

You will need to save your USERS table data first and get the primary ID of last saved and use it as user_id for PROJECTS table and save PROJECT table information creating a PROJECT instance.

view:

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('name',array('type'=>'text','div'=>false));
    echo $this->Form->input('username',array('type'=>'text','div'=>false));
    echo $this->Form->input('password',array('type'=>'password','div'=>'false));
    echo $this->Form->input('email',array('type'=>'email','div'=>false));
    echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
    echo $this->Form->input('phone',array('type'=>'tel','div'=>false));


    // adding associate model name to every field..    
    echo $this->Form->input('Project.name',array('type'=>'text','div'=>false));
    echo $this->Form->input('Project.description',array('type'=>'text','div'=>false));
    echo $this->Form->input('Project.deadline',array('type'=>'date','div'=>false));
    echo $this->Form->input('Project.budget',array('type'=>'num','div'=>false));
    echo $this->Form->submit('Save');
    echo $this->Form->end();

Note: You can disable all 'div' on form creation.. Look documentation for details...

User model:

class User extends AppModel{
   public $hasMany=array('Project');
}

UsersController:

public function add(){
    if($this->request->is('post')){
       $this->User->create();
       if($this->User->save($this->request->data)){// if you association is correct, all associated data will save automatically. 
         $this->Session->setFlash('saved');
         $this->redirect('where you wants to redirect');
       }else{
         $this->Session->setFlash('Something went wrong. Try again');
       }

    }

}

Easy way with saveAll

<!-- View -->
<?php
echo $this->Form->create('User');
echo $this->Form->input('name',array('type'=>'text','div'=>false));
echo $this->Form->input('username',array('type'=>'text','div'=>false));
echo $this->Form->input('password',array('type'=>'password','div'=>false));
echo $this->Form->input('email',array('type'=>'email','div'=>false));
echo $this->Form->input('address',array('type'=>'textarea','div'=>false));
echo $this->Form->input('phone',array('type'=>'tel','div'=>false));

echo $this->Form->input('Project.0.name',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.description',array('type'=>'text','div'=>false));
echo $this->Form->input('Project.0.deadline',array('type'=>'date','div'=>false));
echo $this->Form->input('Project.0.budget',array('type'=>'num','div'=>false));
echo $this->Form->submit('Save');
echo $this->Form->end();
?>

<!-- Controller -->
<?php
class UserController extends AppController
{
    function create()
    {
        if($this->request->is('post', 'put'))
        {
            if($this->User->saveAll($this->request->data))
            {
                // Success - set your flashMessage
            } else {
                // Error - set your flashMessage
            }
        }
    }
}
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top