<?php class nlCategory extends xPDOSimpleObject { public function __construct(& $xpdo) { parent :: __construct($xpdo); } public function addCategory($name='',$pid=0) { // ew. subcategories $this->fromArray(array( 'name' => $name, 'pid' => $pid, )); $this->save(); } } ?>
$cat = $modx->newObject('nlCategory'); $cat->addCategory('Marketing Tools');
<?php /** * @package nobilelibrary */ $base_path = !empty($base_path) ? $base_path : $modx->getOption('core_path').'components/nobilelibrary/'; $modx->addPackage('nobilelibrary',$base_path.'model/'); $cat = $modx->newObject('nlCategory'); // $cat->addCategory('Marketing Tools'); $cat->fromArray(array( 'name' => 'Products Library', 'pid' => 0, )); $cat->save(); ?>
<?xml version="1.0" encoding="UTF-8"?> <model package="nobilelibrary" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM"> <object class="nlFile" table="nl_files" extends="xPDOSimpleObject"> <composite alias="Cart" class="nlCart" local="id" foreign="fid" cardinality="many" owner="local" /> <aggregate alias="Category" class="nlCategory" local="cid" foreign="id" cardinality="one" owner="foreign" /> <field key="cid" dbtype="int" precision="11" phptype="integer" attributes="unsigned" default="0" /> <field key="file" dbtype="varchar" precision="500" phptype="string" null="false" default="" index="index" /> <field key="meta_data" dbtype="text" phptype="string" null="false" default="" /> <field key="mime_type" dbtype="varchar" precision="250" phptype="string" null="false" default="" /> <field key="date" dbtype="date" precision="11" phptype="integer" attributes="unsigned" null="false" default="" /> </object> <object class="nlCategory" table="nl_categories" extends="xPDOSimpleObject"> <composite alias="File" class="nlFile" local="id" foreign="cid" cardinality="many" owner="local" /> <field key="pid" dbtype="int" precision="11" phptype="integer" attributes="unsigned" default="0" /> <field key="name" dbtype="varchar" precision="255" phptype="string" null="false" default="" index="index" /> <field key="subcategories" dbtype="varchar" precision="255" phptype="string" null="false" default="" /> </object> <object class="nlCart" table="nl_carts" extends="xPDOSimpleObject"> <aggregate alias="File" class="nlFile" local="fid" foreign="id" cardinality="one" owner="foreign" /> <field key="uid" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" /> <field key="fid" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" /> </object> </model>
<?php // assuming name is unique, get existing category by name $cat = $modx->getObject('nlCategory', array('name' => $name)); if (!$cat) { // if not found, create a new one $cat = $modx->newObject('nlCategory'); $cat->fromArray(array( 'name' => $name ,'pid' => $pid )); } else { // else it exists, so only update pid // (NOTE: changes will only be saved if the new value is different) $cat->set('pid', $pid); } $saved = $cat->save(); ?>
why is addCategory() a function of nlCategory when an instance of nlCategory is required to use the function.
$cat = $modx->newObject('nlCategory'); // I get instance $cat->addCategory('Marketing Tools'); // use function on that instance
Because getting an instance to see if an instance already exists is wasteful IMO. If you wanted to use a static function on the class, that might be a better approach, then you avoid the unnecessary new instance if you are retrieving an existing instance from the database.
why is addCategory() a function of nlCategory when an instance of nlCategory is required to use the function.
Why shouldn’t I do like that if it works?
$cat = $modx->newObject('nlCategory'); // I get instance $cat->addCategory('Marketing Tools'); // use function on that instance
<?php $modx->loadClass('nlCategory'); $category = nlCategory::addCategory($name, $pid); ?>