orm icon indicating copy to clipboard operation
orm copied to clipboard

Allow _select_ condition in related conditions.

Open itsa-sh opened this issue 12 years ago • 5 comments

I have been attempting to use ORM to do the complex work, in order to build a nice query to execute in the DB class.

for example:

$statement = Model_Parent::query()
    ->related('child')
    ->select(array(
        'id',
        'child.name',
        'name',
    ));

foreach ($some_array_of_fields as $field => $value) {
    // essentially does this, in a round-about way.
    // the flexibility is irrelevent though.
    $statement->or_where($field, 'LIKE', $value)
}

$result = $statement->get_query()->compile();

// This will ultimately return the fields as per `select`, however preceding will be _all_ fields from the `child` related model.

var_dump($result);
die;

I was hoping to be able to do one of the following:

$statement = Model_Parent::query()
    ->related('child', array(
        'select' => array(
            'id',
            'name',
        )
    ))
    ->select(array(
        'id',
        'child.name',
        'name',
    ));

Or using the select define only those fields from the join; to give me only:

t0_id, t1_name, t0_name

itsa-sh avatar Jun 18 '13 16:06 itsa-sh

You can't use the orm to do partial selects as this results in incomplete models.

emlynwest avatar Jun 18 '13 16:06 emlynwest

@itsash-- use the DB class to partially select data. (Excuse any syntax errors / poor code :smile:)

$fields = [
    ['parent.id', 'parent_id'],
    ['parent.name', 'parent_name'],
    ['child.id', 'child_id'],
    ['child.name', 'child_name']
];
$data = DB::select_array($fields)
    ->from('parent')
    ->join('child')->on('parent.child_id', '=', 'child.id')
    ->as_object()
    ->execute();
foreach($data as $d)
{
    $str = $d->parent_id . '\n';
    $str .= $d->parent_name . '\n';
    $str .= $d->child_id . '\n';
    $str .= $d->child_name . '\n';
    print($str);
}

steadweb avatar Jun 18 '13 17:06 steadweb

This can't be done without massive refactoring of the Query class. So it has to move to 2.0

WanWizard avatar Jun 18 '13 21:06 WanWizard

@stevewest : I understand this, but it's not for building orm models, instead to generate the SQL syntax for execution.

@WanWizard: I was assuming the Orm was iterating over the related models' properties and appending them all to the select statement. It does work for the main Orm: Model::query()->select('id') will put id in the select, so i assume it's simple for the related, but if you say it needs re factoring then fine.

itsa-sh avatar Jun 19 '13 09:06 itsa-sh

@itsash-- My bad, lots of people have asked how to do partial selects with orm objects and thought you where wanting the same. Sorry for the misunderstanding.

emlynwest avatar Jun 19 '13 11:06 emlynwest