We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15114
    • 102 Posts
    Hi

    I have an object called "Category". Each category can have a child category. So I defined a composite and an aggregate :

    <aggregate alias="Parent" class="BoutiqueCategorie" local="parent" foreign="id" cardinality="one" owner="foreign" />
    <composite alias="Enfant" class="BoutiqueCategorie" local="id" foreign="parent" cardinality="many" owner="local" />
    


    When I delete a category, I want that all sub-categories to be removed. That partially works.

    If I have this structure :

    Category 1
       |- Category 2
             |- Category 3



    If I remove Category 2, Category 3 is also removed. But if I remove Category 1, Category 2 is also removed but not Category 3.

    Another problem is that if a sub-category can't be removed, the parent category is still removed and the sub-category is left alone
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      This might be a question for Jason... there might be some other relationship or attribute you have to define (?)
        • 22303 MODX Staff
        • 10,725 Posts
        That should work as far as I can tell. Any errors being recorded? Any code to share?
          • 32699 ☆ A M B ☆
          • 427 Posts
          You need to be mindful with the coding.

          Are you doing a getCollection or getCollectionGraph?

          Are you
          $someVar = $modx->getObject('BoutiqueCategorie', array('id'=> $someIdNumber));
          $someVar->remove();
          


          Are your relationships hydrated: http://rtfm.modx.com/display/xPDO20/Hydrating+Fields

          Would $modx->getObjectGraph('BoutiqueCategorie', array('id'=> $someIdNumber)) be a better implementation?

          What does getCollectionGraph return on the category tree?

          These are just some thoughts.
            Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

            Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
            • 15114
            • 102 Posts
            Quote from: opengeek at Jul 08, 2013, 07:31 AM
            That should work as far as I can tell. Any errors being recorded? Any code to share?

            my model :

            <?xml version="1.0" encoding="UTF-8"?>
            <model package="aip_boutique" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
                <object class="BoutiqueCategorie" table="aip_boutique_categories" extends="xPDOSimpleObject">
                    <field key="parent" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                    <field key="order" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" />
                    <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
                    <field key="description" dbtype="text" phptype="string" null="true" default="" />
                    <field key="visible" dbtype="tinyint" phptype="boolean" null="false" default="1" />
                    
                    <aggregate alias="Parent" class="BoutiqueCategorie" local="parent" foreign="id" cardinality="one" owner="foreign" />
            		<composite alias="Enfant" class="BoutiqueCategorie" local="id" foreign="parent" cardinality="many" owner="local" />
            	
                    <validation>
            		    <rule field="name"
            			  name="preventBlank"
            			  type="xPDOValidationRule"
            			  rule="xPDOMinLengthValidationRule"
            			  value="1"
            			  message="aip_boutique.categories.category_err_ns_name"
            		     />
            		</validation>
                </object>
            </model>
            


            my remove processor :

            <?php
            class CategoryRemoveProcessor extends modObjectRemoveProcessor {
            
            	public $classKey = 'BoutiqueCategorie';
            	public $languageTopics = array('aip_boutique:default');
            	public $objectType = 'aip_boutique.category';
            
            	public function beforeRemove() {
            
            		$primaryKey = $this->getProperty($this->primaryKeyField, false);
            		
            		if($this->modx->getCount('BoutiqueArticle', array('category' => $primaryKey)) > 0) {
            			$this->modx->error->failure($this->modx->lexicon('aip_boutique.categories.category_err_remove_count_article'));
            		}
            		
            		return parent::beforeRemove();
            	}
            }
            
            return 'CategoryRemoveProcessor';
            


            and I try to remove my categories from a MODX.tree.
            My remove function :

            removeCategory: function(btn,e) {
            	    if (!this.cm.activeNode) return false;
            	    
            	    MODx.msg.confirm({
            	        text: _('aip_boutique.categories.category.remove_confirm'),
            	        url: this.config.url,
            	        params: {
            	            action: 'mgr/category/remove',
            	            id: this.cm.activeNode.attributes.pk
            	        },
            	        listeners: {
            	            'success': {fn:function(r) { this.refresh(); },scope:this}
            	        }
            	    });
            	}