[李景山php]每天laravel-20161131|BelongsToMany.php-3

    /**
     * Save a new model and attach it to the parent model.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  array  $joining
     * @param  bool   $touch
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function save(Model $model, array $joining = [], $touch = true)
       //public function save (Model $model,array $joining = [],$touch = true)
    {//save a new model and attach it to the parent model
        $model->save([‘touch‘ => false]);// a fixed function use type

        $this->attach($model->getKey(), $joining, $touch);// attach has three parameters
       // first getKey second joining third is touch

        return $model;
    }// return self by class name

    /**
     * Save an array of new models and attach them to the parent model.
     *
     * @param  \Illuminate\Support\Collection|array  $models
     * @param  array  $joinings
     * @return array
     */
    public function saveMany($models, array $joinings = [])
    {//save an array of new models and attach them to the parent model.
        foreach ($models as $key => $model) {
            $this->save($model, (array) Arr::get($joinings, $key), false);
        }//loop models , wrap a function that name is saveMany,

        $this->touchIfTouching();// use a default process.

        return $models;
    }// return this models

    /**
     * Find a related model by its primary key.
     *
     * @param  mixed  $id
     * @param  array  $columns
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|null
     */
    public function find($id, $columns = [‘*‘])
    {// Find a related model by its primary key.
        if (is_array($id)) {
            return $this->findMany($id, $columns);// findMany
        }// if this target is a array ,we will use a function to wrap it.

        $this->where($this->getRelated()->getQualifiedKeyName(), ‘=‘, $id);//default just do it

        return $this->first($columns);
    }// return this fist result with this options

    /**
     * Find multiple related models by their primary keys.
     *
     * @param  mixed  $ids
     * @param  array  $columns
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function findMany($ids, $columns = [‘*‘])
    {// Find multiple related models by their primary keys.
        if (empty($ids)) {
            return $this->getRelated()->newCollection();
        }// if this ids is null or empty we just return a new Collection

        $this->whereIn($this->getRelated()->getQualifiedKeyName(), $ids);// get More ids

        return $this->get($columns);
    }// return this get Columns

    /**
     * Find a related model by its primary key or throw an exception.
     *
     * @param  mixed  $id
     * @param  array  $columns
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function findOrFail($id, $columns = [‘*‘])
    {//Find a related model by its primary key or throw an exception
        $result = $this->find($id, $columns);// this is the find result by option with id and columns

        if (is_array($id)) {// if this id is a aggregate
            if (count($result) == count(array_unique($id))) {
                return $result;// check this result , ok just return it
            }
        } elseif (! is_null($result)) {// null return it self
            return $result;
        }
// default throw something wrong.
        throw (new ModelNotFoundException)->setModel(get_class($this->parent));
    }

    /**
     * Find a related model by its primary key or return new instance of the related model.
     *
     * @param  mixed  $id
     * @param  array  $columns
     * @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
     */
    public function findOrNew($id, $columns = [‘*‘])
    {//Find or new
       //Find a related model by its primary key or return new instance of the related model.
        if (is_null($instance = $this->find($id, $columns))) {
            $instance = $this->getRelated()->newInstance();
        }//whatever we must get this instance

        return $instance;
    }// in the end ,return instance ok

    /**
     * Get the first related model record matching the attributes or instantiate it.
     *
     * @param  array  $attributes
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function firstOrNew(array $attributes)
    {//Get the first related model record matching the attributes or instantiate it.
        if (is_null($instance = $this->where($attributes)->first())) {
            $instance = $this->related->newInstance($attributes);
        }// is null do other

        return $instance;// return this instance
    }

    /**
     * Get the first related record matching the attributes or create it.
     *
     * @param  array  $attributes
     * @param  array  $joining
     * @param  bool   $touch
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function firstOrCreate(array $attributes, array $joining = [], $touch = true)
    {//Get the first related record matching the attributes or create it.
        if (is_null($instance = $this->where($attributes)->first())) {
            $instance = $this->create($attributes, $joining, $touch);
        }// null

        return $instance;
    }//return instance

    /**
     * Create or update a related record matching the attributes, and fill it with values.
     *
     * @param  array  $attributes
     * @param  array  $values
     * @param  array  $joining
     * @param  bool   $touch
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true)
    {//Create or update a related record matching the attributes, and fill it with values.
        if (is_null($instance = $this->where($attributes)->first())) {
            return $this->create($values, $joining, $touch);
        }// is_null

        $instance->fill($values);//fill something

        $instance->save([‘touch‘ => false]);// a save function

        return $instance;
    }// return this instance.

    /**
     * Create a new instance of the related model.
     *
     * @param  array  $attributes
     * @param  array  $joining
     * @param  bool   $touch
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function create(array $attributes, array $joining = [], $touch = true)
    {//Create a new instance of the related model
        $instance = $this->related->newInstance($attributes);// first set the instance.

        // Once we save the related model, we need to attach it to the base model via
        // through intermediate table so we‘ll use the existing "attach" method to
        // accomplish this which will insert the record and any more attributes.
        $instance->save([‘touch‘ => false]);

        $this->attach($instance->getKey(), $joining, $touch);

        return $instance;
    }// attach to create something

    /**
     * Create an array of new instances of the related models.
     *
     * @param  array  $records
     * @param  array  $joinings
     * @return array
     */
    public function createMany(array $records, array $joinings = [])
    {// this function is creates function,yeah
        $instances = [];

        foreach ($records as $key => $record) {
            $instances[] = $this->create($record, (array) Arr::get($joinings, $key), false);
        }

        $this->touchIfTouching();

        return $instances;
    }

    /**
     * Sync the intermediate tables with a list of IDs or collection of models.
     *
     * @param  \Illuminate\Database\Eloquent\Collection|array  $ids
     * @param  bool   $detaching
     * @return array
     */
    public function sync($ids, $detaching = true)
    {//sync to all relation table by ids or models
        $changes = [
            ‘attached‘ => [], ‘detached‘ => [], ‘updated‘ => [],
        ];// set this changes models

        if ($ids instanceof Collection) {
            $ids = $ids->modelKeys();
        }// ids instanceof Collection

        // First we need to attach any of the associated models that are not currently
        // in this joining table. We‘ll spin through the given IDs, checking to see
        // if they exist in the array of current ones, and if not we will insert.
        $current = $this->newPivotQuery()->pluck($this->otherKey);// first get the current

        $records = $this->formatSyncList($ids);// get records

        $detach = array_diff($current, array_keys($records));// get detach

        // Next, we will take the differences of the currents and given IDs and detach
        // all of the entities that exist in the "current" array but are not in the
        // the array of the IDs given to the method which will complete the sync.
        if ($detaching && count($detach) > 0) {
            $this->detach($detach);

            $changes[‘detached‘] = (array) array_map(function ($v) {
                return is_numeric($v) ? (int) $v : (string) $v;
            }, $detach);
        }

        // Now we are finally ready to attach the new records. Note that we‘ll disable
        // touching until after the entire operation is complete so we don‘t fire a
        // ton of touch operations until we are totally done syncing the records.
        $changes = array_merge(
            $changes, $this->attachNew($records, $current, false)
        );

        if (count($changes[‘attached‘]) || count($changes[‘updated‘])) {
            $this->touchIfTouching();
        }

        return $changes;
    }
时间: 2024-08-17 23:55:51

[李景山php]每天laravel-20161131|BelongsToMany.php-3的相关文章

[李景山php]每天laravel-20161130|BelongsToMany.php-2

    /**      * Get the pivot attributes from a model.      *      * @param  \Illuminate\Database\Eloquent\Model  $model      * @return array      */     protected function cleanPivotAttributes(Model $model)     {//Get the pivot attributes from a mode

[李景山php]每天laravel-20161129|BelongsToMany.php-1

namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Da

[李景山php]每天TP5-20170115|thinkphp5-Model.php-8

    /**      * 预载入关联查询 返回模型对象      * @access public      * @param Model     $result 数据对象      * @param string    $relation 关联名      * @return Model      */     public function eagerlyResult($result, $relation)     {         return $this->relation()->

[李景山php]每天laravel-20160920|Writer-2

    //2016-07-20     /**      * Register a file log handler.      *      * @param  string  $path      * @param  string  $level      * @return void      */     public function useFiles($path, $level = 'debug')     {         $this->monolog->pushHandle

[李景山php]每天laravel-20161021|Request.php-2

/**  * Determine if the current request URL and query string matches a pattern.  *  * @param  mixed  string  * @return bool  */ public function fullUrlIs() {// check string like URL     $url = $this->fullUrl();     foreach (func_get_args() as $patter

[李景山php]每天laravel-20161103|CompilerEngine.php-2

    /**      * Handle a view exception.      *      * @param  \Exception  $e      * @param  int  $obLevel      * @return void      *      * @throws $e      */     protected function handleViewException(Exception $e, $obLevel)     {         $e = new E

[李景山php]每天laravel-20161104|Engine.php

<?php namespace Illuminate\View\Engines; abstract class Engine {     /**      * The view that was last to be rendered.      *      * @var string      */     protected $lastRendered;//The view that was last to be rendered.     /**      * Get the last 

[李景山php]每天laravel-20161105|EngineInterface.php

<?php namespace Illuminate\View\Engines; interface EngineInterface {     /**      * Get the evaluated contents of the view.      *      * @param  string  $path      * @param  array   $data      * @return string      */     public function get($path, 

[李景山php]每天laravel-20161106|EngineResolver.php

<?php namespace Illuminate\View\Engines; use Closure; use InvalidArgumentException; class EngineResolver {// engineResolver     /**      * The array of engine resolvers.      *      * @var array      */     protected $resolvers = [];// The array of e