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)
}

Advertisements

content parsing with simplexml_import_dom

$doc = new DOMDocument();
$doc->loadHTML($this->view->content);
$xml = simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath(‘//img’);
$this->view->images = $images;

preg_match_all(‘/]+>/i’,$this->view->content, $result);

foreach($result as $img){
$this->view->content = str_replace($img, ”, $this->view->content);
}

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();

Recursive filtering array in php

$json = $q->execute(array(),Doctrine::HYDRATE_ARRAY);
$filter_key_array = array(‘is_enabled’,’sortorder’,’deleted_at’,’is_deleted’);

$this->sanitizeJson(&$json[0], $filter_key_array);

————
public function sanitizeJson($array,$filter_key_array){
foreach($array as $key => &$value){
if(is_array($value)){
$value = $this->sanitizeJson($value, $filter_key_array);
}else{
if(in_array($key, $filter_key_array)){
unset($array[$key]);
}
}
}
return $array;
}

jquery autocomplete

view

$(“input#store_id”).autocomplete({
source: “/admin/store/get-store-list”,
minLength: 2,
dataType: ‘json’
});

php
$stores = Doctrine_Query::create()
->from(“Stores’)
->where(‘name like ?’, “%{$term}%”)
->execute()
;
$json = array();
foreach($stores as $item){
array_push($json, array(
“label” => $item->name,
“value” => $item->slug
));
}
$this->_helper->json($json);

put code in php without backslash

$js =
<<<HTML

$(“a[id$=FieldBtn]”).bind(‘click’,function(){

var target = $(this).prev();
var nameList = $(this).next().next();

if(target.val() != ”){// if label field is not blank
// check there is same value list or not
// add list
$(this).next().append(‘

  • ‘+ target.val()+’
  • ‘);
    //set result
    $(this).setResult($(this).next().children(), nameList);
    }

    });

    HTML;