We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 50105
    • 13 Posts
    lightweight30 Reply #1, 9 years ago
    I'm trying to create a follow/unfollow system with Ajax and PHP. Created a custom table "follows" and MODX schema.

    Managed to add the user ID and follow ID to the database when user clicks follow.

    My problem is removing a row when the user clicks unfollow, I get an error from the PHP file below.

    Here is my unfollow.php file

    <?php
    
    if (isset($_POST["followid"]) && $_POST["action"] == "unfollow"){
    
    $path = MODX_CORE_PATH . 'components/follows/';
    $result = $modx->addPackage('follows',$path .
            'model/','mgc_');
    
    if (! $result) {
        return 'Failed to add package';
    }
    
    
    $user = $modx->getUser();
    $user_id = $user->get('id');
    
    $follow_id = (int)$_POST["followid"];
    
    $fields = array(
            'user_id' => $user_id,
            'follow_id' => $follow_id
     );	
    
    
     $unfollow= $modx->getObject('Follows', $fields);
    
       if ( $unfollow->remove() == false) {
    
             echo "error";
        }
       else{
    
           echo "ok";
        }
    }
    
    ?>
    


    Any help or advice would be much appreciated.


    • MODXcloud: MODX Revolution 2.3.3-pl (traditional)
    • PHP Version 5.4.34

    This question has been answered by lightweight30. See the first response.

    [ed. note: lightweight30 last edited this post 9 years ago.]
      • 3749
      • 24,544 Posts
      See what happens if you add this as line 26:

      if (! $unfollow) {
         echo "could not get Follows object";
      }


      Does your Follows object extend xPDOObject or xPDOSimpleObject?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 50105
        • 13 Posts
        lightweight30 Reply #3, 9 years ago
        I added it and it does not echo that statement.

        It extends xPDOSimpleObject.

        <?xml version="1.0" encoding="UTF-8"?>
        <model package="follows" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
        	<object class="Follows" table="follows" extends="xPDOSimpleObject">
        		<field key="user_id" dbtype="int" precision="10" phptype="integer" null="false" index="index" />
        		<field key="follow_id" dbtype="int" precision="10" phptype="integer" null="false" />
        
        		<index alias="user_id" name="user_id" primary="false" unique="false" type="BTREE" >
        			<column key="user_id" length="" collation="A" null="false" />
        			<column key="follow_id" length="" collation="A" null="false" />
        		</index>
        	</object>
        </model>
        [ed. note: lightweight30 last edited this post 9 years ago.]
        • discuss.answer
          • 50105
          • 13 Posts
          lightweight30 Reply #4, 9 years ago
          Tried to use getCount() function and it was returning zero.

          So added a primary key to my DB table and changed my schema to below. This resolved my problem.

          <?xml version="1.0" encoding="UTF-8"?>
          <model package="follows" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
          	<object class="Follows" table="follows" extends="xPDOSimpleObject">
          		<field key="user_id" dbtype="int" precision="10" phptype="integer" null="false" index="index" />
          		<field key="follow_id" dbtype="int" precision="10" phptype="integer" null="false" />
          
          		<index alias="id" name="id" primary="true" unique="true" type="BTREE" >
          			<column key="id" length="" collation="A" null="false" />
          		</index>
          	</object>
          </model>
          
            • 3749
            • 24,544 Posts
            Yes, if you use xPDOObject, you have to provide your own 'id' field. You get that automatically with xPDOSimpleObject. I think you get the PK index automatically too, so you don't have to have one in your schema.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting