Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to exclude id(primary key) in ORM find result?
  • This is the code:
    $result = \Model_Test::query()
                  ->select('name')
                  ->where('id', $id)
                  ->get();

    But id is included in the result.
    I tried also this code:
    $result = \Model_Test::query()
                   ->select('name', array('id' => false))
                   ->where('id', $id)
                   ->get();
    But still id is included in the result.

    Thank you.
  • HarroHarro
    Accepted Answer
    An ORM is not a query builder.

    It is a bad thing to use select(),it will create partials objects, and once created, you will never be able to fetch the remaining data. Use it only if you know what you are doing.

    An ORM requires the primary key to be present in every object, otherwise it won't know the state of the object, and can't relate it to anything.

    If you really only want the name, use the DB query builder:

    $result = DB::select('name')
        ->from(\Model_Test
    ::table())
        ->where('id', $id)
        ->execute();

    p.s. If you want an ORM record by it's primary key, use

    $result = \Model_Test::find($id);

    much easier.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion