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

    I have a custom DB table and MODX restful api setup and outputting the table fine.
    api/commentary/

    When I hit this endpoint
    /api/commentary/235

    I would like to return all the matching rows in DB from a field(fixtureID) rather than the item with primary key with the same value.
    It was recommended on the slack channel that I alter the lines below on the read() function.

    $c = $this->getPrimaryKeyCriteria($id);
    $this->object = $this->modx->getObject($this->classKey,$c);


    Can anyone give me a hand? Not sure if there is a better approach.

    Thanks



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

      • 3749
      • 24,544 Posts
      Try something like this:
      $url = $_SERVER['REQUEST_URI'];
      $hasNumber = preg_match('#(\d+)$#', $string, $matches);
      
      if ($hasNumber) {
          $fixtureID = $matches[1];
          $c = array('fixtureID' => $fixtureID);
      } else {
        /* No number at end of string 
           do something else */
      }


        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
      • discuss.answer
        • 50105
        • 13 Posts
        Thanks Bob. Thought a bit more about it and I managed to solve this by modifying the get() and getList().

        If primary key is empty throw exception otherwise call modRestController.getList() not modRestController.read()

        /**
             * Route GET requests
             * @return array
             */
           public function get() {
                $pk = $this->getProperty($this->primaryKeyField);
                if (empty($pk)) {
                     Throw new Exception('Not found', 404); 
        			return false;
                }
                return $this->getList($pk);
            }


        Then have getList accept primary key ID and add $c->where(array('fixtureID' => $id)) to getList()

        /**
             * Abstract method for routing GET requests without a primary key passed. Must be defined in your derivative
             * controller. Handles fetching of collections of objects.
             *
             * @abstract
             * @return array
             */
             public function getList($id) {
                $this->getProperties();
                $c = $this->modx->newQuery($this->classKey);
        		$c->where(array('fixtureID' => $id));
                $c = $this->addSearchQuery($c);
                $c = $this->prepareListQueryBeforeCount($c);


        [ed. note: lightweight30 last edited this post 5 years, 7 months ago.]