本文整理汇总了PHP中Cake\ORM\Table::hasOne方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::hasOne方法的具体用法?PHP Table::hasOne怎么用?PHP Table::hasOne使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Table
的用法示例。
在下文中一共展示了Table::hasOne方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setupFieldAssociations
/**
* Creates the associations between the bound table and every field passed to
* this method.
*
* Additionally it creates a `i18n` HasMany association that will be
* used for fetching all versions for each record in the bound table
*
* @param string $table the table name to use for storing each field version
* @return void
*/
public function setupFieldAssociations($table)
{
$alias = $this->_table->alias();
foreach ($this->_fields() as $field) {
$name = $this->_table->alias() . '_' . $field . '_version';
$target = TableRegistry::get($name);
$target->table($table);
$this->_table->hasOne($name, ['targetTable' => $target, 'foreignKey' => 'foreign_key', 'joinType' => 'LEFT', 'conditions' => [$name . '.model' => $alias, $name . '.field' => $field], 'propertyName' => $field . '_version']);
}
$this->_table->hasMany($table, ['foreignKey' => 'foreign_key', 'strategy' => 'subquery', 'conditions' => ["{$table}.model" => $alias], 'propertyName' => '__version', 'dependent' => true]);
}
示例2: setupFieldAssociations
/**
* Creates the associations between the bound table and every field passed to
* this method.
*
* Additionally it creates a `i18n` HasMany association that will be
* used for fetching all translations for each record in the bound table
*
* @param array $fields list of fields to create associations for
* @param string $table the table name to use for storing each field translation
* @param string $model the model field value
* @param string $strategy the strategy used in the _i18n association
*
* @return void
*/
public function setupFieldAssociations($fields, $table, $model, $strategy)
{
$targetAlias = $this->_translationTable->alias();
$alias = $this->_table->alias();
$filter = $this->_config['onlyTranslated'];
$tableLocator = $this->tableLocator();
foreach ($fields as $field) {
$name = $alias . '_' . $field . '_translation';
if (!$tableLocator->exists($name)) {
$fieldTable = $tableLocator->get($name, ['className' => $table, 'alias' => $name, 'table' => $this->_translationTable->table()]);
} else {
$fieldTable = $tableLocator->get($name);
}
$conditions = [$name . '.model' => $model, $name . '.field' => $field];
if (!$this->_config['allowEmptyTranslations']) {
$conditions[$name . '.content !='] = '';
}
$this->_table->hasOne($name, ['targetTable' => $fieldTable, 'foreignKey' => 'foreign_key', 'joinType' => $filter ? 'INNER' : 'LEFT', 'conditions' => $conditions, 'propertyName' => $field . '_translation']);
}
$conditions = ["{$targetAlias}.model" => $model];
if (!$this->_config['allowEmptyTranslations']) {
$conditions["{$targetAlias}.content !="] = '';
}
$this->_table->hasMany($targetAlias, ['className' => $table, 'foreignKey' => 'foreign_key', 'strategy' => $strategy, 'conditions' => $conditions, 'propertyName' => '_i18n', 'dependent' => true]);
}
示例3: versionAssociation
/**
* Returns association object for all versions or single field version.
*
* @param string|null $field Field name for per-field association.
* @param array $options Association options.
* @return \Cake\ORM\Association
*/
public function versionAssociation($field = null, $options = [])
{
$name = $this->_associationName($field);
if (!$this->_table->associations()->has($name)) {
$model = $this->_config['referenceName'];
if ($field) {
$this->_table->hasOne($name, $options + ['className' => $this->_config['versionTable'], 'foreignKey' => $this->_config['foreignKey'], 'joinType' => 'LEFT', 'conditions' => [$name . '.model' => $model, $name . '.field' => $field], 'propertyName' => $field . '_version']);
} else {
$this->_table->hasMany($name, $options + ['className' => $this->_config['versionTable'], 'foreignKey' => $this->_config['foreignKey'], 'strategy' => 'subquery', 'conditions' => ["{$name}.model" => $model], 'propertyName' => '__version', 'dependent' => true]);
}
}
return $this->_table->association($name);
}
示例4: setupFieldAssociations
/**
* Creates the associations between the bound table and every field passed to
* this method.
*
* Additionally it creates a `i18n` HasMany association that will be
* used for fetching all translations for each record in the bound table
*
* @param array $fields list of fields to create associations for
* @param string $table the table name to use for storing each field translation
* @param array $fieldConditions conditions for finding fields
* @param string $strategy the strategy used in the _i18n association
*
* @return void
*/
public function setupFieldAssociations($fields, $table, $fieldConditions, $strategy)
{
$targetAlias = $this->_translationTable->alias();
$alias = $this->_table->alias();
$filter = $this->_config['onlyTranslated'];
foreach ($fields as $field) {
$name = $alias . '_' . $field . '_translation';
$conditions = [$name . '.model' => $fieldConditions['model'], $name . '.field' => $field];
foreach ($fieldConditions as $fieldName => $fieldValue) {
$conditions[$name . '.' . $fieldName] = $fieldValue;
}
if (!TableRegistry::exists($name)) {
$fieldTable = TableRegistry::get($name, ['className' => $table, 'alias' => $name, 'table' => $this->_translationTable->table()]);
} else {
$fieldTable = TableRegistry::get($name);
}
$this->_table->hasOne($name, ['targetTable' => $fieldTable, 'foreignKey' => 'foreign_key', 'joinType' => $filter ? 'INNER' : 'LEFT', 'conditions' => $conditions, 'propertyName' => $field . '_translation']);
}
$this->_table->hasMany($targetAlias, ['className' => $table, 'foreignKey' => 'foreign_key', 'strategy' => $strategy, 'conditions' => ["{$targetAlias}.model" => $fieldConditions['model']], 'propertyName' => '_i18n', 'dependent' => true]);
}
示例5: testHasOnePlugin
/**
* Test has one with a plugin model
*
* @return void
*/
public function testHasOnePlugin()
{
$options = ['className' => 'TestPlugin.Comments'];
$table = new Table(['table' => 'users']);
$hasOne = $table->hasOne('Comments', $options);
$this->assertInstanceOf('Cake\\ORM\\Association\\HasOne', $hasOne);
$this->assertSame('Comments', $hasOne->name());
$hasOneTable = $hasOne->target();
$this->assertSame('Comments', $hasOne->alias());
$this->assertSame('TestPlugin.Comments', $hasOne->registryAlias());
$options = ['className' => 'TestPlugin.Comments'];
$table = new Table(['table' => 'users']);
$hasOne = $table->hasOne('TestPlugin.Comments', $options);
$this->assertInstanceOf('Cake\\ORM\\Association\\HasOne', $hasOne);
$this->assertSame('Comments', $hasOne->name());
$hasOneTable = $hasOne->target();
$this->assertSame('Comments', $hasOne->alias());
$this->assertSame('TestPlugin.Comments', $hasOne->registryAlias());
}
示例6: testHasOne
/**
* Tests that hasOne() creates and configures correctly the association
*
* @return void
*/
public function testHasOne()
{
$options = ['foreignKey' => 'user_id', 'conditions' => ['b' => 'c']];
$table = new Table(['table' => 'users']);
$hasOne = $table->hasOne('profile', $options);
$this->assertInstanceOf('Cake\\ORM\\Association\\HasOne', $hasOne);
$this->assertSame($hasOne, $table->association('profile'));
$this->assertEquals('profile', $hasOne->name());
$this->assertEquals('user_id', $hasOne->foreignKey());
$this->assertEquals(['b' => 'c'], $hasOne->conditions());
$this->assertSame($table, $hasOne->source());
}
示例7: __construct
/**
* {@inheritDoc}
*/
public function __construct(Table $table, array $config = [])
{
$config['pk'] = $table->primaryKey();
$config['tableAlias'] = (string) Inflector::underscore($table->table());
$table->hasOne('Search.SearchDatasets', ['foreignKey' => 'entity_id', 'conditions' => ['SearchDatasets.table_alias' => $config['tableAlias']], 'dependent' => true]);
parent::__construct($table, $config);
}