Howto get specific tag-object with fuel_model() call

edited February 2017 in News & Announcements
Hello,

I have a view file.
In code I want to retrieve a tag-object filtered by a tagname .
This tag is associated to a object of a simple module.

With this tag-object I want to retrieve all related records from this simple module.
The simple module is associated with the tags module by a has_many Property.

---------------------
What I HAVE
---------------------
I have a solution, but with a foreach-loop over all tags, where I filter by tagname INSIDE the loop.
But this is bad practice and perhaps someday not performant with many records.


// ---------------- my simple module model file------------------------
class simplexys_model extends Base_module_model {
public $has_many = array('tags' => array(FUEL_FOLDER => 'fuel_tags_model'));
..

// ---------------- a record in fuel_tags TABLE ------------------------
slug-column: "my_tagname"

// ---------------- VIEW ------------------------
// my "bad code", works but I don't want to filter inside the loop

$tags = fuel_model('tags');
foreach($tags as $tag) {

if(!empty($tag->slug)) {
if($tag->slug == 'my_tagname') { // <==== I WILL REPLACE THIS LINE where I filter by my tagname
foreach($tag->simplexys as $simplexy) {
echo $simplexy->name;
}
}
}
}


-----------------------
What I TRIED in my VIEW, but does not work
-----------------------

$tag = fuel_model('tags','one',array('slug','my_tagname')); // <===== my TRY to filter the tags by tagname

if (isset($tag->id))
{
$simplexys= $tag->simplexys;
foreach($simplexys as $simplexy )
{
// ===== Note: no if filtering by tagname anymore =====
echo $simplexy->name;
}
}

-----------------------
NOT WORK means - I get an error when the view is loaded:
-----------------------

A Database Error Occurred

Error Number: 1054

Unknown column '0' in 'where clause'

SELECT `fuel_tags`.* FROM (`fuel_tags`) LEFT JOIN `fuel_categories` ON `fuel_categories`.`id` = `fuel_tags`.`category_id` WHERE `0` = 'slug' AND `1` = 'my_tagname' AND `fuel_tags`.`published` = 'yes' LIMIT 1

Filename: /usr/www/..../my_website/fuel/modules/fuel/core/MY_Model.php

Line Number: 478


------------


Best, Gordian

------------

Comments

  • edited 3:16PM
    Try adding the => to make the where condition an assoc array:
    $tag = fuel_model('tags','one',array('slug' => 'my_tagname')); // INSTEAD OF $tag = fuel_model('tags','one',array('slug','my_tagname'));
Sign In or Register to comment.