Doctrine Record Fetch Event

public function postHydrate($event) {
parent::postHydrate($event);
$invoker = $event->data;

$array = array(“to_json”,”type”,”product_ids”);
foreach($array as $arr){
if(isset($invoker->$arr))
$invoker->$arr = Zend_Json::decode($invoker->$arr);
}

}

Advertisements

Doctrine fetch Hydrate type

$sql = ”
SELECT
DISTINCT t2.name as name
FROM tag_index t1, tag t2
WHERE
t1.tid = t2.id
AND t1.nid = {$nid}
AND t1.type = ‘{$type}’
“;

$rs = Doctrine_Manager::getInstance()->getCurrentConnection()->execute($sql)->fetchAll(Doctrine::HYDRATE_RECORD);

::record
[name]
tag
tag2

//HYDRATE_RECORD
object type and fetch with column name

array(2) {
[0] => array(1) {
[“name”] => string(3) “tag”
}
[1] => array(1) {
[“name”] => string(4) “tag2”
}
}

//HYDRATE_ON_DEMAND
simple array

array(2) {
[0] => string(3) “tag”
[1] => string(4) “tag2”
}

//HYDRATE_NONE
object type and
fetch with column name and column index

array(2) {
[0] => array(2) {
[“name”] => string(3) “tag”
[0] => string(3) “tag”
}
[1] => array(2) {
[“name”] => string(4) “tag2”
[0] => string(4) “tag2”
}
}
*
//HYDRATE_ARRAY
object type and fetch with column index
array(2) {
[0] => array(1) {
[0] => string(3) “tag”
}
[1] => array(1) {
[0] => string(4) “tag2”
}
}

//HYDRATE_SINGLE_SCALAR
simple array and fetch boolean type
array(2) {
[0] => bool(true)
[1] => bool(true)
}

Doctrine nested categories

$treeObject = Doctrine_Core::getTable(‘Category’)->getTree();
$treeObject->createRoot($category);

::ADD
$child1 = new Category();
$child1->getNode()->insertAsLastChildOf($category);

::delete
$category = Doctrine_Core::getTable(‘Category’)->findOneByName(‘Child Category 1’);
$category->getNode()->delete();

::move child to the other root
$category = new Category();
$category->name = ‘Root Category 2’;
$category->save();

$categoryTable = Doctrine_Core::getTable(‘Category’);

$treeObject = $categoryTable->getTree();
$treeObject->createRoot($category);

$childCategory = $categoryTable->findOneByName(‘Child Category 1’);
$childCategory->getNode()->moveAsLastChildOf($category);

::examine
$isLeaf = $category->getNode()->isLeaf();
$isRoot = $category->getNode()->isRoot();

$nextSib = $category->getNode()->getNextSibling();
$prevSib = $category->getNode()->getPrevSibling();
$siblings = $category->getNode()->getSiblings();

$hasChildren = $category->getNode()->hasChildren();
$hasParent = $category->getNode()->hasParent();

$firstChild = $category->getNode()->getFirstChild();
$lastChild = $category->getNode()->getLastChild();

$parent = $category->getNode()->getParent();

$children = $category->getNode()->getChildren();

$descendants = $category->getNode()->getDescendants();
$ancestors = $category->getNode()->getAncestors();

$numChildren = $category->getNode()->getNumberChildren();
$numDescendants = $category->getNode()->getNumberDescendants();

Doctrine self relation YAML

Product:
tableName: product
columns:
id:
type: integer(4)
primary: true
autoincrement: true
parent_id:
type: integer(4)
name:
type: string(255)
price:
type: float(10)
description:
type: string()
relations:
Accessories:
type: many
class: Product
local: id
foreign: parent_id

$product = Doctrine::getTable(‘Product’)->find(1);

var_dump($product->id);
var_dump($product->Accessories->toArray());

Doctrine Raw Query

$q = Doctrine_Manager::getInstance()->getCurrentConnection();

$result = $q->execute(

SELECT DISTINCT category_id
, GROUP_CONCAT(DISTINCT people_id) as people_id
, c.name as cname
, GROUP_CONCAT( DISTINCT p2.name) as pname
FROM pictures p
LEFT JOIN category c
ON p.category_id = c.id
LEFT JOIN people p2
ON p.people_id = p2.id
GROUP BY p.category_id
ORDER BY p.category_id asc

);