开发者

CakePHP Gurus: is my complex saveAll scenario possible?

I have an event management app, where event owners can import a list of email addresses to add to their event. The app should create a Member record for each email address, and then create an EventsMember record linking each new Member to the Event.

The whole reason for doing this with saveAll() is to (a) minimi开发者_Go百科ze the number of SQL calls of course (there could be thousands of Members per event, and I don't want to design the import feature to run one SQL statement per new Member :), and (b) leverage transactions. I'm open to other ways to do this that leverage CakePHP efficiencies.

So I am reading the list in from a file and creating an array to pass to the saveAll() method of the Member model; I know I can add every Member with an array like this:

$data = Array(
  '0' => Array('email' => 'nobody@nowhere.com'),
  '1' => Array('email' => 'somebody@nowhere.com'),
  ...
);
$this->Member->saveAll($data);

and this is working fine. But I want to be able to have the associated EventsMember records created at the same time, but I'm not sure what format the data should take, or if it's even possible. I tried:

$data = Array(
  '0' => Array(
    'email' => 'nobody@nowhere.com'),
    'Event' => Array('id' => 10);
  ),
  '1' => Array(
    'email' => 'somebody@nowhere.com'),
    'Event' => Array('id' => 10);
  ),
  ...
);
$this->Member->saveAll($data);

and

$data = Array(
  '0' => Array(
    'email' => 'nobody@nowhere.com'),
    'Event' => Array(
      '0' => Array('id' => 10);
    )
  ),
  '1' => Array(
    'email' => 'somebody@nowhere.com'),
    'Event' => Array(
      '0' => Array('id' => 10);
    )
  ),
  ...
);
$this->Member->saveAll($data);

but they both only created the Member records, and ignored the embedded Event data.

I know that my relationships are working correctly, as other CakePHP features (automatic joins and recursive finds, cascading deletes, etc.) are working great.

Thanks for your insights!


Well, I'm not a cakephp guru, but when saving relations you shouldn't use "EventsMember" just "Events" (or the name of the Model) for example you got Member so it would be like

        $data = array(

            'Member' => array('email' => 'johndoe@google.com'),
            'Event' => array( // many events array of records
                'Event' => array('event_id', 1), // this is a new record
                'Event' => array('event_id', 3) // second record to relate to johndoe
            )
        );
$this->Member->saveAll($data);

The Member model will take care of all relation, so if the Member model has set the relation properly like you mentioned it should work.


In the end I had to combine the methods of saving multiple records, and saving HABTM records as outlined by allenskd. The process looks like this:

$data = array(
  '0' => array(
    'Member' => array('email' => 'nobody@nowhere.com'),
    'Event' => array('id' => 10)
  ),
  '1' => array(
    'Member' => array('email' => 'somebody@nowhere.com'),
    'Event' => array('id' => 10)
  ),
  ...
);
$this->Member->saveAll($data);

Now I have a slightly different problem, which is how to get extra information into the EventsMember record, but that's for a different post :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜