We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 818
    • 23 Posts
    How do I delete using a query?

    that doesn’t work:

    $group= $modx->newObject(’PostContactsGroups’);
    $group->fromArray(array(
    ’group_id’ => ’1’,
    ’contact_id’ => ’2’
    ));
    $group->remove();

    Adding works with:
    $group->save();

    Also I’m confused with newObject and newQuery , when should I use them.

    thanks

      • 17499 ☆ A M B ☆
      • 872 Posts
      Hello alaing,

      It works with the save method because your telling xpdo to save your new (newObject) object.
      To retreive it, you to get the concerned object qith the getObject method : http://rtfm.modx.com/display/xPDO20/xPDO.getObject

      example:
      <?php
      //Your group id
      $id = 1;
      
      //Then retreive the object to remove
      $group = $modx->getObject('PostContactsGroups', array(
      'group_id' => $id,
      ));
      $group->remove();
      
        • 22303 MODX Staff
        • 10,725 Posts
        FYI, you can use xPDO::removeObject() and xPDO::removeCollection() to delete objects without instantiating them; it simply uses the criteria you pass to generate the DELETE statements.
          • 17499 ☆ A M B ☆
          • 872 Posts
          removeCollection work with newQuery and is used like getCollection ?
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: lossendae at Nov 24, 2010, 12:14 PM

            removeCollection work with newQuery and is used like getCollection ?
            Yes, it works with xPDOQuery objects as well as raw criteria; most methods of xPDOQuery objects do not really apply to DELETE statements though FWIW.

            e.g.
            <?php
            $removed = $modx->removeCollection('modResource', array('deleted' => true, 'OR:published:=' => false));
            ?>

            This would delete all the Resources in MODx that were deleted or unpublished.
              • 818
              • 23 Posts
              Quote from: lossendae at Nov 24, 2010, 11:44 AM

              Hello alaing,

              It works with the save method because your telling xpdo to save your new (newObject) object.
              To retreive it, you to get the concerned object qith the getObject method : http://rtfm.modx.com/display/xPDO20/xPDO.getObject

              example:
              <?php
              //Your group id
              $id = 1;
              
              //Then retreive the object to remove
              $group = $modx->getObject('PostContactsGroups', array(
              'group_id' => $id,
              ));
              $group->remove();
              



              I tried that before but getting "Call to a member function remove() on a non-object in"
                • 17499 ☆ A M B ☆
                • 872 Posts
                can you post your table structure or your xml schema ?
                  • 818
                  • 23 Posts
                  Quote from: lossendae at Nov 24, 2010, 03:51 PM

                  can you post your table structure or your xml schema ?

                  here is the xml schema

                  <?xml version="1.0" encoding="UTF-8"?>
                  <model package="wishper_post" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM">
                  <object class="Post" table="post" extends="xPDOSimpleObject">
                  <field key="internalKey" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
                  <field key="post_time" dbtype="int" precision="10" phptype="integer" null="false" default="0" />
                  <field key="post_type" dbtype="char" precision="4" phptype="string" null="false" />
                  <field key="post_subject" dbtype="varchar" precision="100" phptype="string" null="false" />
                  <field key="post_text" dbtype="mediumtext" phptype="string" null="false" />
                  <field key="viewed_time" dbtype="int" precision="10" phptype="integer" null="false" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  </object>
                  <object class="PostAttach" table="post_attach" extends="xPDOSimpleObject">
                  <field key="post_id" dbtype="int" precision="10" phptype="integer" null="true" index="index" />
                  <field key="attachment" dbtype="varchar" precision="45" phptype="string" null="true" />
                  <field key="attache_type" dbtype="char" precision="15" phptype="string" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  <index alias="post_id" name="post_id" primary="false" unique="false">
                  <column key="post_id" collation="A" null="true" />
                  </index>
                  </object>
                  <object class="PostAttachTemp" table="post_attach_temp" extends="xPDOSimpleObject">
                  <field key="internalKey" dbtype="int" precision="10" phptype="integer" null="false" />
                  <field key="attachment" dbtype="varchar" precision="45" phptype="string" null="true" />
                  <field key="attach_caption" dbtype="varchar" precision="60" phptype="string" null="false" />
                  <field key="attache_type" dbtype="char" precision="15" phptype="string" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  </object>
                  <object class="PostComments" table="post_comments" extends="xPDOSimpleObject">
                  <field key="post_id" dbtype="int" precision="10" phptype="integer" null="true" index="index" />
                  <field key="internalKey" dbtype="int" precision="10" phptype="integer" null="false" />
                  <field key="comment_time" dbtype="int" precision="10" phptype="integer" null="true" />
                  <field key="comment" dbtype="text" phptype="string" null="true" />
                  <field key="rating_up" dbtype="int" precision="6" phptype="integer" null="true" />
                  <field key="rating_down" dbtype="int" precision="6" phptype="integer" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  <index alias="post_id" name="post_id" primary="false" unique="false">
                  <column key="post_id" collation="A" null="true" />
                  </index>
                  </object>
                  <object class="PostContacts" table="post_contacts" extends="xPDOSimpleObject">
                  <field key="internalKey" dbtype="int" precision="10" phptype="integer" null="true" index="index" />
                  <field key="first_name" dbtype="varchar" precision="60" phptype="string" null="true" />
                  <field key="last_name" dbtype="varchar" precision="60" phptype="string" null="false" />
                  <field key="email" dbtype="varchar" precision="45" phptype="string" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  <index alias="internalKey" name="internalKey" primary="false" unique="false">
                  <column key="internalKey" collation="A" null="true" />
                  </index>
                  </object>
                  <object class="PostContactsGroups" table="post_contacts_groups" extends="xPDOSimpleObject">
                  <field key="group_id" dbtype="int" precision="10" phptype="integer" null="true" index="index" />
                  <field key="contact_id" dbtype="varchar" precision="45" phptype="string" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  <index alias="group_id" name="group_id" primary="false" unique="false">
                  <column key="group_id" collation="A" null="true" />
                  </index>
                  </object>
                  <object class="PostGroups" table="post_groups" extends="xPDOSimpleObject">
                  <field key="internalKey" dbtype="int" precision="10" phptype="integer" null="false" index="index" />
                  <field key="group_name" dbtype="varchar" precision="45" phptype="string" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  <index alias="internalKey" name="internalKey" primary="false" unique="false">
                  <column key="internalKey" collation="A" null="false" />
                  </index>
                  </object>
                  <object class="PostInbox" table="post_inbox" extends="xPDOSimpleObject">
                  <field key="internalKey" dbtype="varchar" precision="45" phptype="string" null="true" />
                  <field key="post_id" dbtype="varchar" precision="45" phptype="string" null="true" />
                  <field key="sender_id" dbtype="int" precision="10" phptype="integer" null="false" />
                  <field key="post_type" dbtype="char" precision="2" phptype="string" null="false" />
                  <field key="inbox_status" dbtype="char" precision="1" phptype="string" null="true" />

                  <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true">
                  <column key="id" collation="A" null="false" />
                  </index>
                  </object>
                  </model>