本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::getForeignKey方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getForeignKey方法的具体用法?PHP Model::getForeignKey怎么用?PHP Model::getForeignKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::getForeignKey方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: aggregate
/**
* Aggregates data handling to the subclasses.
*
* @param array $data the handling internediate data.
* @param array|Model $value the handling Model instance.
* @return array the resulting intermediate Format instance.
*/
protected function aggregate(array $data, Model $value)
{
$data[self::KEY_OF_TABLE_NAME] = $value->getTable();
$data[self::KEY_OF_FOREIGN_KEY] = $value->getForeignKey();
$data[self::KEY_OF_OTHER_KEY] = $value->getOtherKey();
return $data;
}
示例2: firstOrCreateTranslation
/**
* Creates a translation.
*
* @param Model $locale
* @param string $text
* @param Model $parentTranslation
*
* @return Model
*/
protected function firstOrCreateTranslation(Model $locale, $text, $parentTranslation = null)
{
// We'll check to see if there's a cached translation
// first before we try and hit the database.
$cachedTranslation = $this->getCacheTranslation($locale, $text);
if ($cachedTranslation instanceof Model) {
return $cachedTranslation;
}
// Check if auto translation is enabled. If so we'll run
// the text through google translate and
// save it, then cache it.
if ($parentTranslation && $this->autoTranslateEnabled()) {
$googleTranslate = new TranslateClient();
$googleTranslate->setSource($parentTranslation->locale->code);
$googleTranslate->setTarget($locale->code);
try {
$text = $googleTranslate->translate($text);
} catch (ErrorException $e) {
// Request to translate failed, set the text
// to the parent translation
$text = $parentTranslation->translation;
} catch (UnexpectedValueException $e) {
// Looks like something other than text was passed in,
// we'll set the text to the parent translation
// for this exception as well
$text = $parentTranslation->translation;
}
}
$translation = $this->translationModel->firstOrCreate([$locale->getForeignKey() => $locale->getKey(), $this->translationModel->getForeignKey() => isset($parentTranslation) ? $parentTranslation->getKey() : null, 'translation' => $text]);
// Cache the translation so it's retrieved faster next time
$this->setCacheTranslation($translation);
return $translation;
}
示例3: getJoinClause
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $locale
* @param string $alias
* @return callable
*/
protected function getJoinClause(Eloquent $model, $locale, $alias)
{
return function (JoinClause $join) use($model, $locale, $alias) {
$primary = $model->getTable() . '.' . $model->getKeyName();
$foreign = $model->getForeignKey();
$langKey = $model->getLocaleKey();
$join->on($alias . '.' . $foreign, '=', $primary)->where($alias . '.' . $langKey, '=', $locale);
};
}
示例4: createWhere
/**
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
*/
protected function createWhere(EloquentBuilder $builder, Eloquent $model)
{
if ($model->getOnlyTranslated() && $model->shouldFallback()) {
$key = $model->getForeignKey();
$primary = "{$this->i18nTable}.{$key}";
$fallback = "{$this->i18nTable}_fallback.{$key}";
$ifNull = $builder->getQuery()->compileIfNull($primary, $fallback);
$builder->whereRaw("{$ifNull} is not null");
}
}
示例5: buildDictionary
/**
* Build model dictionary keyed by the relation's foreign key.
*
* @param \Illuminate\Database\Eloquent\Collection $results
* @return array
*/
protected function buildDictionary(Collection $results)
{
$dictionary = array();
$foreign = $this->farParent->getForeignKey();
// First we will create a dictionary of models keyed by the foreign key of the
// relationship as this will allow us to quickly access all of the related
// models without having to do nested looping which will be quite slow.
foreach ($results as $result) {
$dictionary[$result->{$foreign}][] = $result;
}
return $dictionary;
}
示例6: firstOrCreateTranslation
/**
* Creates a translation.
*
* @param Model $locale
* @param string $text
* @param Model $parentTranslation
*
* @return Model
*/
protected function firstOrCreateTranslation(Model $locale, $text, $parentTranslation = null)
{
// We'll check to see if there's a cached translation
// first before we try and hit the database.
$cachedTranslation = $this->getCacheTranslation($locale, $text);
if ($cachedTranslation instanceof Model) {
return $cachedTranslation;
}
// Check if auto translation is enabled. If so we'll run
// the text through google translate and
// save it, then cache it.
if ($parentTranslation && $this->autoTranslateEnabled()) {
$this->client->setSource($parentTranslation->locale->code);
$this->client->setTarget($locale->code);
try {
$text = $this->client->translate($text);
} catch (ErrorException $e) {
// Request to translate failed, set the text
// to the parent translation.
$text = $parentTranslation->translation;
} catch (UnexpectedValueException $e) {
// Looks like something other than text was passed in,
// we'll set the text to the parent translation
// for this exception as well.
$text = $parentTranslation->translation;
}
}
if ($parentTranslation) {
// If a parent translation is given we're looking for it's child translation.
$translation = $this->translationModel->firstOrNew([$locale->getForeignKey() => $locale->getKey(), $this->translationModel->getForeignKey() => $parentTranslation->getKey()]);
} else {
// Otherwise we're creating the parent translation.
$translation = $this->translationModel->firstOrNew([$locale->getForeignKey() => $locale->getKey(), 'translation' => $text]);
}
if (empty($translation->getAttribute('translation'))) {
// We need to make sure we don't overwrite the translation
// if it exists already in case it was modified.
$translation->setAttribute('translation', $text);
}
if ($translation->isDirty()) {
$translation->save();
}
// Cache the translation so it's retrieved faster next time
$this->setCacheTranslation($translation);
return $translation;
}
示例7: joinIntermediate
/**
* Join pivot or 'through' table.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation
* @param string $type
* @return void
*/
protected function joinIntermediate(Model $parent, Relation $relation, $type)
{
if ($relation instanceof BelongsToMany) {
$table = $relation->getTable();
$fk = $relation->getForeignKey();
} else {
$table = $relation->getParent()->getTable();
$fk = $table . '.' . $parent->getForeignKey();
}
$pk = $parent->getQualifiedKeyName();
if (!$this->alreadyJoined($join = (new Join($type, $table))->on($fk, '=', $pk))) {
$this->query->joins[] = $join;
}
}
示例8: only
/**
* set the clauses of the sql from the ids array
* for a where in statement
*
* @param array $ids
* @return null
*/
public function only(array $ids)
{
$tableName = $this->model->getTable();
$this->setJoin([$tableName, $this->table . '.' . $this->model->getForeignKey(), '=', $tableName . '.id']);
$this->setWhereIn([$tableName . '.id', $ids]);
}