{"id":1345,"date":"2015-01-31T16:58:31","date_gmt":"2015-01-31T08:58:31","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=1345"},"modified":"2015-01-31T18:15:30","modified_gmt":"2015-01-31T10:15:30","slug":"eloquent-crud%ef%bc%9a__construct","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=1345","title":{"rendered":"Eloquent CRUD\uff1a__construct"},"content":{"rendered":"<p>\u5047\u8a2d\u6211\u5011\u5728\u5beb\u4e00\u500b\u96fb\u5b50\u5546\u52d9\u7db2\u7ad9\u3002\u5efa\u7acb\u8a02\u55ae\u7684\u7b2c\u4e00\u6b65\u5927\u6982\u9577\u9019\u6a23\uff1a<\/p>\n<pre>\r\n$order = new Order();\r\n<\/pre>\n<p>\u6216\u662f<\/p>\n<pre>\r\n$order = new Order(array('amount'=> 500, 'serialNumber' => '201501300001'));\r\n<\/pre>\n<p>\u80cc\u5f8c\u767c\u751f\u4e86\u4ec0\u9ebc\u4e8b\u5462\uff1f\u8b93\u6211\u5011\u4e00\u4e00\u4f86\u770b\u3002<\/p>\n<p>\u9996\u5148\u7576\u7136\u662f\u5efa\u69cb\u5f0f\uff1a<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new Eloquent model instance.\r\n\t *\r\n\t * @param  array  $attributes\r\n\t * @return void\r\n\t *\/\r\n\tpublic function __construct(array $attributes = array())\r\n\t{\r\n\t\t$this->bootIfNotBooted();\r\n\r\n\t\t$this->syncOriginal();\r\n\r\n\t\t$this->fill($attributes);\r\n\t}\r\n\r\n<\/pre>\n<p>\u7b2c\u4e00\u6b65\u5148\u6aa2\u67e5\u662f\u5426\u9700\u8981\u300c\u555f\u52d5\u300d\uff1a<\/p>\n<pre>\r\n\t\/**\r\n\t * Check if the model needs to be booted and if so, do it.\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tprotected function bootIfNotBooted()\r\n\t{\r\n\t\t$class = get_class($this);\r\n\r\n\t\tif ( ! isset(static::$booted[$class]))\r\n\t\t{\r\n\t\t\tstatic::$booted[$class] = true;\r\n\r\n\t\t\t$this->fireModelEvent('booting', false);\r\n\r\n\t\t\tstatic::boot();\r\n\r\n\t\t\t$this->fireModelEvent('booted', false);\r\n\t\t}\r\n\t}\r\n\r\n<\/pre>\n<p>\u5229\u7528\u4e00\u500b\u985e\u5225\u7684\u975c\u614b\u9663\u5217\u7d00\u9304\u662f\u5426\u5df2\u555f\u52d5\uff0c\u4e26\u4e14\u89f8\u767c\u555f\u52d5\u524d\u8207\u555f\u52d5\u5f8c\u7684\u4e8b\u4ef6\u3002<br \/>\n\u770b\u4f86\u4e00\u500b\u985e\u5225\u5728\u4e00\u6b21request\u5167\u53ea\u6703\u88ab\u555f\u52d5\u4e00\u6b21\u3002\u555f\u52d5\u5728\u505a\u4ec0\u9ebc\u5462\uff1f<\/p>\n<pre>\r\n\t\/**\r\n\t * The \"booting\" method of the model.\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tprotected static function boot()\r\n\t{\r\n\t\t$class = get_called_class();\r\n\r\n\t\tstatic::$mutatorCache[$class] = array();\r\n\r\n\t\t\/\/ Here we will extract all of the mutated attributes so that we can quickly\r\n\t\t\/\/ spin through them after we export models to their array form, which we\r\n\t\t\/\/ need to be fast. This will let us always know the attributes mutate.\r\n\t\tforeach (get_class_methods($class) as $method)\r\n\t\t{\r\n\t\t\tif (preg_match('\/^get(.+)Attribute$\/', $method, $matches))\r\n\t\t\t{\r\n\t\t\t\tif (static::$snakeAttributes) $matches[1] = snake_case($matches[1]);\r\n\r\n\t\t\t\tstatic::$mutatorCache[$class][] = lcfirst($matches[1]);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tstatic::bootTraits();\r\n\t}\r\n\r\n\r\n<\/pre>\n<p>\u6587\u4ef6\u4e2d\u6709\u63d0\u5230<a href=\"http:\/\/laravel.com\/docs\/4.2\/eloquent#accessors-and-mutators\" title=\"Accessors &#038; Mutators\">Accessors &#038; Mutators<\/a><br \/>\n\u539f\u4f86\uff0c\u555f\u52d5\u662f\u70ba\u4e86\u652f\u63f4\u5ba2\u88fd\u5316\u7684\u5c6c\u6027\u8b80\u53d6\u3001\u5beb\u5165\u529f\u80fd\u3002<br \/>\n\u6700\u5f8c\u4e00\u53e5\u8ddfPHP\u7684traits\u529f\u80fd\u6709\u95dc\uff0c\u4f60\u9700\u8981\u5148\u4e86\u89e3PHP traits\u3002<br \/>\nbootTraits\u6703\u6aa2\u67e5\u6240\u6709traits\u3001\u770b\u662f\u5426\u6709\u9700\u8981\u300c\u555f\u52d5\u300d\u7684\u90e8\u4efd\u3002\u8209\u4f8b\u4f86\u8aaa\uff0c<a href=\"http:\/\/laravel.com\/docs\/4.2\/eloquent#soft-deleting\" title=\"Soft Deleting\">Soft Deleting<\/a>\u5c31\u662f\u5229\u7528\u4e86traits\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Boot all of the bootable traits on the model.\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tprotected static function bootTraits()\r\n\t{\r\n\t\tforeach (class_uses_recursive(get_called_class()) as $trait)\r\n\t\t{\r\n\t\t\tif (method_exists(get_called_class(), $method = 'boot'.class_basename($trait)))\r\n\t\t\t{\r\n\t\t\t\tforward_static_call([get_called_class(), $method]);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n<\/pre>\n<p>\u6ce8\u610fclass_uses_recursive\u4e26\u4e0d\u662fPHP\u5167\u5efa\u51fd\u6578\uff0c\u662fLarave\u81ea\u5b9a\u7fa9\u7684helper\u8f14\u52a9\u51fd\u6578\u3002<br \/>\n\u518d\u4f86\u662f\u5efa\u69cb\u5f0f\u7b2c\u4e8c\u53e5\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre>\r\n\t\/**\r\n\t * Sync the original attributes with the current.\r\n\t *\r\n\t * @return $this\r\n\t *\/\r\n\tpublic function syncOriginal()\r\n\t{\r\n\t\t$this->original = $this->attributes;\r\n\r\n\t\treturn $this;\r\n\t}\r\n\r\n<\/pre>\n<p>\u4f60\u6703\u767c\u73feoriginal\u8ddfattributes\u90fd\u9084\u53ea\u662f\u7a7a\u9663\u5217\u3002\u5728\u5efa\u69cb\u5f0f\u57f7\u884c\u9019\u53e5\uff0c\u4e4d\u770b\u4e4b\u4e0b\u6c92\u6709\u610f\u7fa9\uff1f<br \/>\n\u6211\u8a62\u554f\u904eLaravel\u539f\u4f5c\u8005\uff0c\u9019\u662f<a href=\"https:\/\/github.com\/laravel\/framework\/pull\/7179\">\u4ed6\u7684\u7b54\u8986<\/a>\u3002<br \/>\n\u7ffb\u95b1<a href=\"http:\/\/laravel.com\/docs\/4.2\/eloquent\">\u6587\u4ef6<\/a>\uff0c\u6703\u767c\u73fe\u5176\u5be6\u53ef\u4ee5override attributes\u9663\u5217\u3002<\/p>\n<p>\u5efa\u69cb\u5f0f\u7684\u6700\u5f8c\u4e00\u53e5\uff0c\u4e5f\u662f\u552f\u4e00\u6709\u5c0d\u50b3\u5165\u5efa\u69cb\u5f0f\u7684\u53c3\u6578\u505a\u8655\u7406\u7684\u65b9\u6cd5\u4f86\u4e86\uff1a<\/p>\n<pre>\r\n\t\/**\r\n\t * Fill the model with an array of attributes.\r\n\t *\r\n\t * @param  array  $attributes\r\n\t * @return $this\r\n\t *\r\n\t * @throws \\Illuminate\\Database\\Eloquent\\MassAssignmentException\r\n\t *\/\r\n\tpublic function fill(array $attributes)\r\n\t{\r\n\t\t$totallyGuarded = $this->totallyGuarded();\r\n\r\n\t\tforeach ($this->fillableFromArray($attributes) as $key => $value)\r\n\t\t{\r\n\t\t\t$key = $this->removeTableFromKey($key);\r\n\r\n\t\t\t\/\/ The developers may choose to place some attributes in the \"fillable\"\r\n\t\t\t\/\/ array, which means only those attributes may be set through mass\r\n\t\t\t\/\/ assignment to the model, and all others will just be ignored.\r\n\t\t\tif ($this->isFillable($key))\r\n\t\t\t{\r\n\t\t\t\t$this->setAttribute($key, $value);\r\n\t\t\t}\r\n\t\t\telseif ($totallyGuarded)\r\n\t\t\t{\r\n\t\t\t\tthrow new MassAssignmentException($key);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn $this;\r\n\t}\r\n<\/pre>\n<p>\u9996\u5148\u6aa2\u67e5\u6240\u6709\u5c6c\u6027\u662f\u5426\u90fd\u88ab\u300c\u9632\u8b77\u300d\u3002\u9019\u662f\u907f\u514d\u4f7f\u7528\u8005\u76f4\u63a5\u628a\u700f\u89bd\u5668\u4e1f\u904e\u4f86\u7684value\uff0c\u76f4\u63a5\u585e\u7d66Eloquent\u7576\u5c6c\u6027\u3001\u9020\u6210\u8cc7\u5b89\u6f0f\u6d1e\u7684\u8a2d\u8a08\u3002\u53c3\u95b1<a href=\"http:\/\/laravel.com\/docs\/4.2\/eloquent#mass-assignment\">Mass Assignment<\/a><\/p>\n<pre>\r\n\t\/**\r\n\t * Determine if the model is totally guarded.\r\n\t *\r\n\t * @return bool\r\n\t *\/\r\n\tpublic function totallyGuarded()\r\n\t{\r\n\t\treturn count($this->fillable) == 0 && $this->guarded == array('*');\r\n\t}\r\n\r\n<\/pre>\n<p>\u63a5\u8457fillableFromArray\u6703\u6aa2\u67e5\u53c3\u6578\u9663\u5217\u4e2d\uff0c\u6709\u54ea\u4e9b\u662f\u53ef\u4ee5\u5beb\u5165\u7684\u5c6c\u6027\uff1a<\/p>\n<pre>\r\n\t\/**\r\n\t * Get the fillable attributes of a given array.\r\n\t *\r\n\t * @param  array  $attributes\r\n\t * @return array\r\n\t *\/\r\n\tprotected function fillableFromArray(array $attributes)\r\n\t{\r\n\t\tif (count($this->fillable) > 0 && ! static::$unguarded)\r\n\t\t{\r\n\t\t\treturn array_intersect_key($attributes, array_flip($this->fillable));\r\n\t\t}\r\n\r\n\t\treturn $attributes;\r\n\t}\r\n<\/pre>\n<p>\u4f60\u6703\u767c\u73fe\u6709unguarded\u8b8a\u6578\u53ef\u4ee5\u95dc\u9589\u9632\u8b77\u6a5f\u5236\u3002\u539f\u59cb\u78bc\u4e2d\u6709\u4e09\u500b\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u9019\u500b\u8b8a\u6578\uff0c\u5b98\u65b9\u6587\u4ef6\u6c92\u6709\u5beb\u9032\u53bb\u3002<br \/>\narray_intersect_key\u8ddfarray_flip\u7684\u5de7\u5999\u642d\u914d\u904b\u7528\uff0c\u627e\u5230\u4e86\u53ef\u5beb\u5165\u7684\u5c6c\u6027\u3002<\/p>\n<p>\u63a5\u8457\u82e5\u901a\u904eisFillable\uff0c\u5c31setAttribute\u3002<br \/>\n\u70ba\u4ec0\u9ebc\u8981\u518d\u6b21\u6aa2\u67e5isFillable\u5462\uff1f\u4e0d\u662f\u90fd\u7528fillableFromArray\u53ea\u627e\u51fa\u53ef\u5beb\u5165\u7684\u5c6c\u6027\u4e86\u55ce\uff1f<\/p>\n<pre>\r\n\t\/**\r\n\t * Determine if the given attribute may be mass assigned.\r\n\t *\r\n\t * @param  string  $key\r\n\t * @return bool\r\n\t *\/\r\n\tpublic function isFillable($key)\r\n\t{\r\n\t\tif (static::$unguarded) return true;\r\n\r\n\t\t\/\/ If the key is in the \"fillable\" array, we can of course assume that it's\r\n\t\t\/\/ a fillable attribute. Otherwise, we will check the guarded array when\r\n\t\t\/\/ we need to determine if the attribute is black-listed on the model.\r\n\t\tif (in_array($key, $this->fillable)) return true;\r\n\r\n\t\tif ($this->isGuarded($key)) return false;\r\n\r\n\t\treturn empty($this->fillable) && ! starts_with($key, '_');\r\n\t}\r\n\r\n<\/pre>\n<p>\u539f\u4f86\u662f\u8981\u6aa2\u67e5\u6709\u6c92\u6709\u5728guarded\u9663\u5217\u88e1\u9762\u3002<\/p>\n<p>\u518d\u4f86\uff0c\u7d42\u65bc\u8981\u5beb\u5165\u5c6c\u6027\u4e86\uff1a<\/p>\n<pre>\r\n\t\/**\r\n\t * Set a given attribute on the model.\r\n\t *\r\n\t * @param  string  $key\r\n\t * @param  mixed   $value\r\n\t * @return void\r\n\t *\/\r\n\tpublic function setAttribute($key, $value)\r\n\t{\r\n\t\t\/\/ First we will check for the presence of a mutator for the set operation\r\n\t\t\/\/ which simply lets the developers tweak the attribute as it is set on\r\n\t\t\/\/ the model, such as \"json_encoding\" an listing of data for storage.\r\n\t\tif ($this->hasSetMutator($key))\r\n\t\t{\r\n\t\t\t$method = 'set'.studly_case($key).'Attribute';\r\n\r\n\t\t\treturn $this->{$method}($value);\r\n\t\t}\r\n\r\n\t\t\/\/ If an attribute is listed as a \"date\", we'll convert it from a DateTime\r\n\t\t\/\/ instance into a form proper for storage on the database tables using\r\n\t\t\/\/ the connection grammar's date format. We will auto set the values.\r\n\t\telseif (in_array($key, $this->getDates()) && $value)\r\n\t\t{\r\n\t\t\t$value = $this->fromDateTime($value);\r\n\t\t}\r\n\r\n\t\t$this->attributes[$key] = $value;\r\n\t}\r\n\r\n<\/pre>\n<p>\u4e00\u958b\u59cb\u7684Mutator\u6aa2\u67e5&#8230;\u55ef\u6211\u9084\u6c92\u641e\u61c2\uff0c\u5148\u7565\u904e\u3002<br \/>\n\u63a5\u8457\u662f\u6aa2\u67e5\u662f\u5426\u70ba\u65e5\u671f\u5c6c\u6027\u3001\u8f49\u6210\u5b57\u4e32\u3002\u5c31\u662f<a href=\"http:\/\/laravel.com\/docs\/4.2\/eloquent#date-mutators\">Date Mutator<\/a>\u9019\u6bb5\u7684\u63cf\u8ff0\u3002<br \/>\n\u6c92\u554f\u984c\u7684\u8a71\uff0c\u5c31\u5b58\u9032attributes\u9663\u5217\u88e1\u982d\u3002<br \/>\n\u54a6\uff1f\u600e\u9ebc\u662f\u5b58\u9032attributes\u9663\u5217\u3001\u4e0d\u662f\u76f4\u63a5\u8a2d\u6210\u7269\u4ef6\u5c6c\u6027\u5440\uff1f<br \/>\n\u90a3\u6211\u53d6\u51fa\u5c6c\u6027\u6642\u4e0d\u5c31\u8981\u900f\u904eattributes\u9663\u5217\uff1f<br \/>\n\u5225\u64d4\u5fc3\uff0c\u5225\u5fd8\u4e86PHP\u6709__get\u9019\u7a2e\u795e\u5947\u51fd\u6578\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Dynamically retrieve attributes on the model.\r\n\t *\r\n\t * @param  string  $key\r\n\t * @return mixed\r\n\t *\/\r\n\tpublic function __get($key)\r\n\t{\r\n\t\treturn $this->getAttribute($key);\r\n\t}\r\n<\/pre>\n<h3>\u8907\u7fd2<\/h3>\n<p>\u597d\uff0c\u6700\u5f8c\u6211\u5011\u4f86\u8907\u7fd2\u4e00\u904d\u3002<br \/>\nEloquent\u5efa\u69cb\u5f0f\u6703\u4f9d\u5e8f\u57f7\u884c\u4e09\u500b\u65b9\u6cd5\u3002<br \/>\n\u4e00\u3001\u57f7\u884c\u555f\u52d5\u7a0b\u5e8f\uff08\u652f\u63f4\u5ba2\u88fd\u5316\u5c6c\u6027\u8b80\u5beb\u3001\u57f7\u884c\u6240\u6709traits\u7684\u555f\u52d5\u7a0b\u5e8f\uff09<br \/>\n\u4e8c\u3001\u8f09\u5165\u4f7f\u7528\u8005\u81ea\u5b9a\u7fa9\u5c6c\u6027\uff08\u4f7f\u7528\u8005\u5b9a\u7fa9\u7684$this->attributes\u9663\u5217\uff09<br \/>\n\u4e09\u3001\u5c07\u50b3\u5165\u5efa\u69cb\u5f0f\u7684\u53c3\u6578\u5beb\u5165\u70ba\u5c6c\u6027<\/p>\n<p>\u4ee5\u4e0a\u5927\u81f4\u5c31\u662f\u4e00\u500bEloquent\u5b50\u985e\u5225\u5728\u5efa\u69cb\u5f0f\u4e2d\u6703\u505a\u7684\u4e8b\u60c5\u3002\u662f\u4e0d\u662f\u6bd4\u60f3\u50cf\u4e2d\u8907\u96dc\u5730\u591a\u5462\uff1a\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5047\u8a2d\u6211\u5011\u5728\u5beb\u4e00\u500b\u96fb\u5b50\u5546\u52d9\u7db2\u7ad9\u3002\u5efa\u7acb\u8a02\u55ae\u7684\u7b2c\u4e00\u6b65\u5927\u6982\u9577\u9019\u6a23\uff1a $order = new Order(); \u6216\u662f  &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=1345\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">Eloquent CRUD\uff1a__construct<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_mi_skip_tracking":false},"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1345"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1345"}],"version-history":[{"count":13,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1345\/revisions"}],"predecessor-version":[{"id":1382,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1345\/revisions\/1382"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}