Question

  1. What do these getParent() and getChildren() do? Does it all have to do with parent-child table relationship in database? If so, where should I put these - parent_type and parent_id, in Zend_Model or Zend_Db_Table?

  2. How do I call getParent()/getChildren() in controller and what they will return.

  3. Can I use Zend normal convention for table relations.

Was it helpful?

Solution

I already figured it out.I found that getParent(<itemType>) and getChildren(<itemType>) return Core_Model_Item_Abstract. But you must set $_parent_type and $_children_types[array] in your child and parent table model respectively (not in _model_dbtable).

You also must make a getChildrenSelectOfItemType($obj,$params = array() in your child table model(_Model_DbTable_),which returns a Zend_Db_Select and you have to define what this select will return.

Here ItemType in SE means moduleName_itemname (e.g. album_photo ).

Assuming account and bugs having 1:n relation, hence the following has to be in MyModule_Model_DbTable_Bugs.

public function getChildrenSelectOfMyModuleAccount($obj,$params = array())
    {

    $select = $this->select();
    $select ->where('account_id = ?', $obj->getIdentity());

    return $select;
    }

}

Then you can get the related children by (in your controller) $account=Engine_Api::_()->getItem('mymodule_account', 1); //1 = matching account_id
$bug=$account->getChildren('mymodule_bug'); //return Core_Model_Item_Abstract or row obj
.

Getting parent table rows is less complicated, declare $_parent_type in child table and parent_id column must be present in database child table - bugs.

$bug=Engine_Api::_()->getItem('mymodule_bug', 1); //1 = bug_id $account=$bug->getParent();** //return Core_Model_Item_Abstract/row obj (don't forget to put a reference column (parent_id) in db bugs table with matching id)

If you set "$_parent_is_owner=true" in MyModule_Model_Bug (without declaring $_owner_type and $_parent_type), the user will become the parent, hence getParent will return the user

One most important thing to know is

Subject, Viewer and Owner

in socialengine context...

Viewer = anyone who is viewing the page(mostly logged in user).e.g. John is a logged-in user who is viewing a photo -a subject.

Subject= Subject is viewed by viewer and/or Viewer actually views the subject. e.g. if user John views an album/photo, album/photo is subject.

Owner=Owner owns an item/subject. e.g. John is owner of his photo(a subject).If John is viewing Jerry's photo, subject is Jerry's photo but owner is Jerry.

OTHER TIPS

1) Yes, they for parent-children relationship, but not via Zend native mechanisms (_dependentTables, _referenceMap and so on)

You can find these methods here: abstract class Core_Model_Item_Abstract extends Engine_Db_Table_Row implements Core_Model_Item_Interface (application/modules/Core/Model/Item/Abstract.php)

Look for methods parameters, they important.

2) Usually they using for retrieve database row / rows. For example look into Group_PhotoController::editAction():

// get photo by id (engine4_group_photos.photo_id)

$photo = Engine_Api::_()->core()->getSubject();

// find parent by group_id (engine4_group_photos.group.id)

$group = $photo->getParent('group');

We get group row object (Group_Model_Group) here.

You can overwrite these methods of course. See for example here: class Album_Model_Photo extends Core_Model_Item_Abstract::getParent($type = null)

3) You may try, but it can be tricky, because SE change a lot from Zend framework codebase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top