简体   繁体   中英

How to ignore fields when fetching results using Propel?

I need to ignore fields instead of select fields in fetch data or reverse select fields.

Is that possible? If yes, how?

Regarding to the documentation, it's not possible: http://propelorm.org/reference/model-criteria.html#getting-columns-instead-of-objects

But you can do it on your own.

Built an array of your fields (based on your peer class) and remove those you don't need when you build your query

$fields = MyTablePeer::$fieldKeys[BasePeer::TYPE_PHPNAME];

/**
   will give you (for example):

   array (
     'Id' => 0, 
     'Name' => 1, 
     'Content' => 2, 
   )
 */

// remove unwanted column
unset($fields['Name']);

$items = MyTableQuery::create()
  ->select(array_keys($fields))
  ->find();
}

For Propel version 2 you can do the following (in the same vein as j0k's answer):

$fields = MyTableTableMap::getFieldNames("phpName");

/**
will give you (for example):

array (
    'Id,
    'Name',
    'Content'
)
*/

// remove unwanted columns
$fields = array_values(array_diff($fields, ["Name"]));

$items = MyTableQuery::create()
    ->select(array_keys($fields))
    ->find();

This can easily be turned into a function if you need to do this often:

function exclude_fields($class, $exclude) {
    $tableMap = $class::TABLE_MAP;
    $fields = $tableMap::getFieldNames("phpName");
    return array_values(array_diff($fields, $exclude));
}

$items = MyTableQuery::create()
    ->select(exclude_fields(MyTable::class, ["Name"]))
    ->find();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM