0
votes

In CakePHP I have two models: Clients & Tickets. A client can have many tickets and a ticket can only have 1 client.

When adding a new ticket I want to automatically create a new client by only entering a name. So the form would be:

Name = "Name client" >> Name should be save in Client table en new client_ID in Ticket table Info = Ticket information >> Save in Ticket table. "Save"

I'm not sure how this works. I have associations in the model and tried to saveAll but there is no data stored in the Client table. And how do I get the ID in the Ticket table?

Hope someone can point me in the right direction. I've searched for other answers but cant seem to find a solution. Is saveAll the right way to do this?

2
What is your Client validation rules? - Mindaugas Norvilas
Add code of action and requested array - Moyed Ansari

2 Answers

0
votes

You should set a php condition before insert values into the table, the sql request 4 exemple return the numbers of repetition of the same ticket_id so if the returned value is greater than 1 the request can't execute else the request insert the values The function to count number of rows : mysql_num_rows('request');

0
votes

You can use the callback methods.

If you have defined your relation in the Ticket model with belongsTo Client, it will be accessible from that model.

public $belongsTo = array(
    'Client' => array(
        'className' => 'Client',
        'foreignKey' => 'client_id',
    ),
);

So, if you want to make a new client before saving the ticket, you can do:

public function beforeSave ($options = array()) {
    $this->data['Client']['name'] = $someVar;
    $this->Client->save($this->data);
    $this->data[$this->alias]['client_id'] = $this->Client->getInsertID();
    return true;
}