Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow _select_ condition in related conditions. #272

Open
itsa-sh opened this issue Jun 18, 2013 · 5 comments
Open

Allow _select_ condition in related conditions. #272

itsa-sh opened this issue Jun 18, 2013 · 5 comments

Comments

@itsa-sh
Copy link

itsa-sh commented Jun 18, 2013

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

@emlynwest
Copy link
Contributor

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

@steadweb
Copy link

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

$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);
}

@WanWizard
Copy link
Member

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

@itsa-sh
Copy link
Author

itsa-sh commented Jun 19, 2013

@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.

@emlynwest
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants