Nilesh Kale
Nilesh Kale

Reputation: 233

Reading/Deleting related data/entites in Lithium 0.11 & MongoDB

I have a Holidays model that hasOne Itineraries object. The Itineraries object has a holidays_id as the foreign key (implied by Li3 convention) and a array of objects that store information for each day of the itinerary. I'm using Lithium 0.11 and MongoDB.

  class Holidays extends \lithium\data\Model {

    public $hasOne = array('Itineraries'); 
}


   class Itineraries extends \lithium\data\Model {

    //Relationships
    public $belongsTo = array('Holidays');
}

Controller:

    public function saveitinerary()
{   

    $holidays_id = $this->request->data['holidays_id'];
    $holiday = Holidays::find($holidays_id, array('with' => 'Itineraries'));

    if($holiday->Itineraries)
    {
        Itineraries::remove($holiday->itinerary._id);   
    }

    //save the new itinerary data with added holidays_id as FK.
    $a = array('holidays_id' => $this->request->data['holidays_id'], 'Itineraries' => $this->request->data['itineraries']);

    $success = false;
    $itinerary = Itineraries::create($a);
    $success = $itinerary->save();
    if(!$success) 
        $log->LogDebug("Could not add itinerary for in db for holiday id:" . $holidays_id );
    return $itinerary->to('array');

Here is the sequence of events: 1. The Holidays document is created in MongoDB collection on a previous page and saved successfully. The _id is passed to the backbone based frontend. The Itineraries object is null (not added/saved) when creating the Holidays object.

  1. On subsequent page, the user selects the itinerary and a AJAX/JSON call is made to the controller (saveitinerary), passing in the holidays_id and the array of objects. Each object represents each day of the holiday.

  2. The saveitinerary() fetches the current Holidays document in $holiday, checks if it hasOne of the Itineraries object 'related' to it, if it is then deletes it and adds the new itinerary from the new JSON posted.

PROBLEMS:

  1. Is $holiday->Itineraries the correct way to check the 'joined' data in the Holidays model?

1a. Is Itineraries::remove($holiday->Itineraries._id) the correct way to remove the existing hasOne related object.

  1. At present, it always comes out as null, even when in the database Itineraries objects are created with the holidays_id and other attributes.

  2. It appears that there is almost no relationship 'created' as Itineraries is never picked up, even though documents are created in the Itineraries collection (verified using MongoDB shell)

Upvotes: 2

Views: 248

Answers (1)

rapzo
rapzo

Reputation: 31

AFAIK relations is not implemented for MongoDb, which makes some sense since it's a non-relational Db, however, linking is a common practice and as i look into your example, it's what you're trying to do, so, $hasOne, $hasMany and $belongs work only on RDBs such as MySQL and so on.

On the other hand, Nate Abele posted this gist with how to build those type of one-to-one and one-to-many relations. It may need some tweaking (DocumentArray was merged with DocumentSet for a start) but will do the job.

Shout if you need help with that snippet.

Upvotes: 2

Related Questions