Skip to content

Instantly share code, notes, and snippets.

@Langmans
Last active April 4, 2016 11:47
Show Gist options
  • Select an option

  • Save Langmans/d0960dbfe8b80d3e61b39e1f22792e85 to your computer and use it in GitHub Desktop.

Select an option

Save Langmans/d0960dbfe8b80d3e61b39e1f22792e85 to your computer and use it in GitHub Desktop.
bla.php
<?php $sql = sprintf("SELECT GROUP_CONCAT(dc.document_category_id) as cat_ids, d.*
FROM %s dc
LEFT JOIN %s d ON(d.id = dc.document_id)
GROUP BY d.id
ORDER BY d.added",
DocumentCategorization::getBacktickTableName(),
Document::getBacktickTableName()
);
/** @var As_Database $db */
$db = Document::getDb();
$result = $db->query($sql);
/** @var DocumentCategorizationCollection[] $cats_done */
$cats_done = array();
while ($row = $result->getRow()) {
$doc = Document::constructByFields($row);
foreach (explode(',', $row['cat_ids']) as $cat_id) {
/** @var DocumentCategory $cat */
$cat = $this->get($cat_id);
if (!isset($cats_done[$cat_id])) {
$cats_done[$cat_id] = new DocumentCategorizationCollection();
$cat->setDocumentCategorization_Collection($cats_done[$cat_id]);
}
$z = new DocumentCategorization();
$z->setDocument_Object($doc);
$z->setDocumentCategory_Object($cat);
$cats_done[$cat_id]->add($z);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment