标签:laravel
/** * 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; }
本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1855677
[李景山php]每天laravel-20161131|BelongsToMany.php-3
标签:laravel
原文地址:http://jingshanls.blog.51cto.com/3357095/1855677