Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Query extended fields

edited June 2015 in Framework

How do I get records with a value for a specific extended field? i.e. query->where (extended_field != '')

Comments

  • Extended fields are stored as a JSON object, so they're not indexed by MySQL and there's not a great way to fetch values by them.

    If it's for a new model you're creating, I would make those fields part of the actual schema and not part of the extended fields.

    If it's for something already built into the CMS (e.g., blog posts), I would build my own index for that field using Elefant's hook system, like this:

    1. Create the hook for adding and updating posts in the index on add and edit (apps/myapp/handlers/hook/index_post.php):

    <?php
    
    if (! $this->internal) die;
    
    // Parse 'page' field for blog post ID
    if (preg_match ('|^blog/post/([0-9]+)/|', $this->data['page'], $regs)) {
        $id = (int) $regs[1];
        $post = new blog\Post ($id);
        $extended_fields = (array) $post->ext ();
    
        // TODO: Index post based on $extended_fields['field_name']
    }
    

    2. Create the hook for removing posts from the index on delete (apps/myapp/handlers/hook/remove_post.php):

    <?php
    
    if (! $this->internal) die;
    
    // Parse 'page' field for blog post ID
    if (preg_match ('|^blog/post/([0-9]+)/|', $this->data['page'], $regs)) {
        $id = (int) $regs[1];
    
        // TODO: Delete post from index
    }
    

    3. Add your handlers to the [Hooks] section of conf/config.php:

    blog/add[] = myapp/hook/index_post
    blog/edit[] = myapp/hook/index_post
    blog/delete[] = myapp/hook/remove_post
    

    That should let you keep an index of specific custom fields and search based on that, keeping track of post IDs for an indexed value. Then you can fetch those IDs and just add a where clause like this:

    ->where ('id in(' . join (',', $ids) . ')')
    

    Hope that helps!

  • Thanks.

Sign In or Register to comment.