Jump to content

Recommended Posts

Posted

I added this method to Packages, so you can look for a package based on meta fields.

It even includes some hackery to look up values in serialized data.

 

        /**
         * Searches packages that contain the given meta field
         *
         * @param string $key They service field key to search
         * @param string $value The service field value to search
         * @param string $subkey The key to look for in serialised meta
         * @return array An array of stdClass objects, each containing a service
         */
        public function searchPackageMeta($key, $value, $subkey=null) {
                $module_id=1;
                $this->Record = $this->getPackages();
                $qry = $this->Record->innerJoin("package_meta", "package_meta.package_id", "=", "packages.id", false)->
                        group("packages.id")->
                        where("package_meta.key", "=", $key)->
                        where("package_meta.value", "=", $value);
                if($subkey) {
                        return $qry->orLike("package_meta.value", sprintf('%%s:%d:"%s";s:%d:"%s";%%', strlen($subkey), $subkey, strlen($value), $value))->fetchAll();
                } else {
                        return $qry->orLike("package_meta.value", sprintf('%%:%%:%%;s:%d:"%s";%%', strlen($value), $value))->fetchAll();
                }
        }

 

Example usage:

 

find packages that include .com NameCheap domains.

 

blesta.api('packages/searchPackageMeta', {'key': 'tlds', 'value': '.com'})

Find all SSL certificates

 

blesta.api('packages/searchPackageMeta', {'key': 'type', 'value': 'ssl'})

Get all 33" washing machines

 

blesta.api('packages/searchPackageMeta', {'key': 'dimensions', 'value': '33', 'subkey': 'diameter'})

 

Posted

Looks like you have an unused $module_id (probably remnants from testing?). And the group on packages.id is extraneous, as Packages::getPackages() already groups on this field. But otherwise looks good.

Posted
        /**
         * Searches packages that contain the given meta field
         *
         * @param string $key They service field key to search
         * @param string $value The service field value to search
         * @param string $subkey The key to look for in serialised meta
         * @return array An array of stdClass objects, each containing a service
         */
        public function searchPackageMeta($key, $value, $subkey=null) {
                $this->Record = $this->getPackages();
                $qry = $this->Record->innerJoin("package_meta", "package_meta.package_id", "=", "packages.id", false)->
                        where("package_meta.key", "=", $key)->
                        where("package_meta.value", "=", $value);
                if($subkey) {
                        return $qry->orLike("package_meta.value", sprintf('%%s:%d:"%s";s:%d:"%s";%%', strlen($subkey), $subkey, strlen($value), $value))->fetchAll();
                } else {
                        return $qry->orLike("package_meta.value", sprintf('%%:%%:%%;s:%d:"%s";%%', strlen($value), $value))->fetchAll();
                }
        }

 

Ok, fixed that.

 

I think this is a really usefull thing to have, so if you'd be willing to include it in Blesta, I'm happy to give you all the permissions and copyrights and whatnot to do so.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...