We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17838
    • 82 Posts
    I went through this:
    http://svn.modxcms.com/docs/display/revolution/Using+Custom+Database+Tables+in+your+3rd+Party+Components

    I made 3 tables and schema for them. Each table - one class in model/

    I was trying to extend them:

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


    in my snippet:

    $cat = $modx->newObject('nlCategory');
    $cat->addCategory('Marketing Tools');


    I get 2 records instead of one. Is it advisable to leave those classes as they are or it’s ok to customize them?

    --
    Regards
    MM
      • 26903
      • 1,336 Posts
      Guessing a bit here but try extending from xPDOObject rather than xPDOSimpleObject. I think the latter extends from xPDOObject and assumes an auto_increment field exists in the table.
        Use MODx, or the cat gets it!
        • 17838
        • 82 Posts
        I’ve extended it from xPDOObject and I still get duplicate data. My table has auto_increment id field.

        class ’nlCategory’ is a table class which was generated by build script (http://svn.modxcms.com/docs/display/revolution/Using+Custom+Database+Tables+in+your+3rd+Party+Components).

        So I wanted to customize ’nlCategory’ class - make some shortcut functions.

        if I try like that, i get just one record as it should be:
        Ok with this code I still get 2 records
        <?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();
        
        ?>
          • 17838
          • 82 Posts
          So I have general problem with duplicate data:

          My schema looks like that:
          <?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>
          • First, make the name field on nlCategory have index="unique" (or you can make name + pid a unique index if that is the intention) and second, why is addCategory() a function of nlCategory when an instance of nlCategory is required to use the function. You need to see if the record exists before attempting to add one, then you can decide to skip/update an existing one based on your needs:
            <?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();
            ?>

              • 17838
              • 82 Posts
              Ok, I’ll do as you advised to prevent from data duplication.
              it’s strange: I found out that it happens (i get two identical rows) only when I hit REFRESH button in a browser, not if i just enter a page with a snippet call.

              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
              • Quote from: maciej.m at Feb 24, 2010, 04:41 PM

                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

                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.
                <?php
                $modx->loadClass('nlCategory');
                $category = nlCategory::addCategory($name, $pid);
                ?>