jonathangeiger / kohana-jelly

See the link below for the most up-to-date code
https://github.com/creatoro/jelly
MIT License
146 stars 34 forks source link

Easier way possible to get one-column results in an array? #81

Closed glamorous closed 14 years ago

glamorous commented 14 years ago

Hi,

While developing a site I came accross this "issue". To recieve only one column of a specefic model as array I need this:

$header_images = Jelly::select('event')->header()->select('posterimg')->execute()->as_array(NULL,'posterimg');

As you can see, I say twice that I only need 'posterimg'. Ok this thing should work fine without "->select('posterimg')" but then it would first select all my columns (not such a good thing to performance)

Can their be an easier way to do this or isn't this an issue to find a workaround?

glamorous commented 14 years ago

While experimenting with this I found a "bug" or eather way, this wasn't the expected result. In my model I have this:

class Model_Page extends Jelly_Model
{
public static function initialize(Jelly_Meta $meta)
{
    $meta->fields(array(
        'id' => new Field_Primary,
        'seo' => new Field_BelongsTo,
        'timestamp' => new Field_Timestamp(array(
            'auto_now_create' => TRUE,
            'auto_now_update' => TRUE,
        )),
        'uri' => new Field_Slug,
        'name' => new Field_String,
        'body' => new Field_Text,
    ))
        ->load_with(array('seo'))
        ->name_key('uri');
}
}

Note the "->load_with(array('seo'))"

When I do this:

echo Jelly::select('page')->select('uri');

I see this query:

SELECT rcc_pages.id AS id, rcc_pages.seo_id AS seo, rcc_pages.timestamp AS timestamp, rcc_pages.uri AS uri, rcc_pages.name AS name, rcc_pages.body AS body, rcc_seo.id AS :seo:id, rcc_seo.keywords AS :seo:keywords, rcc_seo.description AS :seo:description, rcc_seo.robots AS :seo:robots, rcc_pages.uri FROM rcc_pages LEFT JOIN rcc_seo ON (rcc_pages.seo_id = rcc_seo.id)

But I expected this:

SELECT rcc_pages.uri FROM rcc_pages

Is this the normal behaviour? I expected that the query didn't mind the ->load_with when you use ->select('column')

jonathangeiger commented 14 years ago

Can their be an easier way to do this or isn't this an issue to find a workaround?

That's probably the easiest way so far. I don't see many feasible ways to make this shorter.

Regarding your other question:

Is this the normal behaviour? I expected that the query didn't mind the ->load_with when you use ->select('column')

Yes. load_with always does the with(). That's the point of it. I can see how...

SELECT rcc_pages.uri FROM rcc_pages

...might make more sense, but then if load_with is only going to happen under certain circumstances, what are those circumstances?

glamorous commented 14 years ago

In any request I do, I need the load_with. Only in this one particular query for my array, I don't need it. For now I deleted my load_with and call it everywhere where I need it (pretty mucht always except that only one :) )

I can live with it this "overload" to code but if their is a solution for it, that should be great ;-)

leth commented 14 years ago

sounds like you need a 'without()' method :P