Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Getting an object for a row using find()
I am trying to get only a single row that matches a WHERE criteria.
$category = array_slice(Model_Category::find('all', array('where' => array(array('name', $name)), 'limit' => 1)),0,1);
$data['category'] = $category;
I am using array_slice and using $category to get the object value by default, as the find() return an associative array. I don't want an associative array.
By this way, I am use $category->name in the view.
get_one() does not allow to specify the where condition. So how best I can do this to get only row row?
If you only want one result back, use 'first' instead of 'all', get the the first object found.
And I don't understand your remark about get_one(). Offcourse it does, but get() and get_one() are chained query methods, and have nothing to do with find().
$data['category'] = Model_Category::query()->where('name', '=', $name)->get_one();
Thakns. I am still trying to understand the fuelPHP way of doing things.
No problem, I think it boils down to personal preference. Some like the array notation, some method chaining.
Fuel v2 will most likely not support find() anymore (at least not in the current complex way), so you might want to use method chaining now, to avoid having to change later.
Hi, I am having a similar problem.
I am trying this but I only get one row when I should get 3.
$data['clubs'] = Model_Clubs::query()->where('id', '=', $user_id)->get();
Also the data seems to be bloated with the model object bits - is there a nice way to strip the "
[_is_new:protected] => [_frozen:protected] => [_sanitization_enabled:protected] => [_data:protected]...etc." from my results?
Any help is greatly appreciated.
What I am asking is...
If I use this:
$arr = DB::query('select * from clubs where id = '.$user_id)->execute();
then, how do I get the same results but using the
ORM/CRUD/MODEL bits ???
I thought it is better to use the models but I just can't seem to get what I need.
How can a query on 'id' (which I assume is the PK) return 3 rows? And on there other hand, if there are 3, the get() should return 3, this query is as simple as they come.
So $data['clubs'] should be an array with 3 Model_Clubs objects.
In contrast, that DB::query() doesn't return an array (so $arr as variable is misleading), it returns a single collection object containing the query results.
There is nothing bloated (or at least not what you think), you're interpreting the result of a var_dump() or so completely wrong. Most of the data in the object is static (and only in memory only once for all model objects), all dynamic data in the object is needed for the ORM to work.
Why is it bothering you?
In this case my clubs table is a one to many relationship - where a member can belong to one or more clubs.
Yes I did a var_dump or similar.
My clubs table has a number of columns and I wanted a list of just the club_id but I was not sure if I needed another column so I just did a "select *". I was hoping to use the Model to easily select the data I need. I know the model object is used for a great number of things but is there a way to strip the result down to a simple array?
I am an older programmer, back when data storage and memory was at a premium. I just like to keep my data to a minimum. Also I was thinking about storing some of the results into Session variables.
Ok, so 'id' is the foreign key?
An ORM model has an to_array() method, which can give you the object properties into an array. You can not run a query using ORM that returns an array, it's simply not what an ORM is for. The ORM also doesn't really like subselects, due to the caching mechanism. But if you need an object only once, you can use select() on an ORM query too.
I understand your statement about age (I've passed 50
very well, but the additional data stored in the object is acually quite limited. A few booleans, and a copy of the properties (in order to track changes to property or relation values). Most of the other data you see on a var_dump() is actually linked in by reference, so apart from a bit of memory for the pointer, it doesn't use any.
Add a Comment
It looks like you're new here. If you want to get involved, click one of these buttons!
Apply for Membership
↳ Job Board
↳ Installation & Setup
↳ Tips and Tutorials
↳ Code share
In this Discussion