@emnsen
C_Parents diye bir tablo görüyorum. Muhtemelen bu tablo gereksiz. Kategoriler gibi bir şey kullanacaksan bu işi çok kolaylaştıran baum node eklentisini kullan. Tek bir model üzerinden kategori olan modelleri yönetebilirsin.
Veritabanı tabloların iyi tasarlamalısın. İlişkilerini ihtiyaçlarını göre kurmazsan, işini çok zorlaştırırsın.
Kategoriler yönetmek bu eklentiyle çocuk oyuncağı oluyor:
örnek:
public function testGetBaseRoot() {
$a = ProCategory::create(array('name' => 'A'));
$b = ProCategory::create(array('name' => 'B'));
$c = ProCategory::create(array('name' => 'C'));
$d = ProCategory::create(array('name' => 'D'));
// a > b > c > d
$b->makeChildOf($a);
$c->makeChildOf($b);
$d->makeChildOf($c);
$a->reload(); $b->reload(); $c->reload(); $d->reload();
$this->assertEquals('A', $b->getBaseRootName()); //testi geçer
$this->assertEquals('A', $c->getBaseRootName()); //testi geçer
$this->assertEquals('A', $d->getBaseRootName()); //testi geçer
}
Yukardaki örnekte A nesnesi kök kategoridir. D ise en uçtaki dal kategoridir. D'den A kategorisine ulaşmak için şu methodlar yazılabilir.
/**
*
* @return \ProCategory
*/
public function getBaseRoot() {
$root = $this->getRoot();
if (is_null($root)) {
return null;
}
return $root;
}
/**
*
* @return \ProCategory
*/
public function getBaseRootName() {
$root = $this->getBaseRoot();
if (is_null($root)) {
return null;
}
return $root->name;
}
/**
*
* @return \ProCategory
*/
public function getBaseRootId() {
$root = $this->getBaseRoot();
if (is_null($root)) {
return null;
}
return $root->id;
}
Aynı mantığı ilişkiler üzerinden de yapabilirsin.
Mesala Ürünler diye bir modelimiz olsun. Bir de Ürünler ait fiyat, imajlar vb ilişkiler olsun
/**
* Description of Product
*
*/
class Product extends Eloquent {
/**
* Defining relation to ProductImages
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ProductImages() {
return $this->hasMany('ProductImage', 'product_id', 'id');
}
/**
* Defining relation to ProductColor
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function ProductColors() {
return $this->hasMany('ProductColor', 'product_id', 'id');
}
/**
* Defining Relation of Product Category Model
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function ProCategory() {
return $this->belongsTo('ProCategory', 'procategory_id', 'id');
}
/**
* Defining Relation of ProductPrice Models
*
* @return \Illuminate\Database\Eloquent\Relations\hasOne
*/
public function ProductPrice() {
return $this->hasOne('ProductPrice', 'product_id', 'id');
}
}
Muhtemelen projemizde her şeye Product modeli ile erişmek isteyecez. Çünkü bu yol en pratik yoldur.
Örneğin sitemizin ön yüzünde ürünlerin listelendiği bir tablo yapmak istiyoruz. Bunun için ürünün görsellerinde ilk fotoğraflarını çekmek isteyelim. Dikkat edersen Product modelinin birden fazla görseli olabilir. Yani hasMany ilişkisi var.
Bunu yapmak için Product modelline şu methodu ekleyelim..
// Product Modeli
/**
* To get first imege path if it is existed
*
* @return string
*/
public function getFirstImage() {
$images = $this->ProductImages;
foreach ($images as $v) {
return $v->path;
}
return $this->defaultImage;
}
Product modelini kullanırken ilk görseli çekmek çocuk oyuncağı
$model = Product::find(23);
echo $model->getFirstImage()// /file/upload/foo.png
Mesala Ürünlerin görsellerini güncellemek istiyoruz. Bunun için de benzer bir yöntem kullanacaz.
// Product Modeli
/**
* To add image path for Product
*
* @param array $data
* @return boolean
*/
public function addImage(array $data=array()) {
foreach ($data as $value) {
$return = $this->ProductImages()->create(['path' => $value]);
if ($return === false || $return === null) {
return false;
}
}
return true;
}
/**
* To update images model
*
* @param array $data Paths of images
* @return boolean
*/
public function updateImage(array $data=array()) {
$this->DeleteAllImageModel();
if (count($data) === 0) {
return;
}
return $this->addImage($data);
}
/**
* To delete all ımages...
*/
public function DeleteAllImageModel() {
$models = $this->ProductImages === null ? array() : $this->ProductImages;
$deleted =array();
foreach ($models as $v) {
$deleted[] = $v->delete();
if (in_array(false, $deleted)) {
throw new \Exception('Görseller silinemedi!');
}
}
}
Şimdi ProductImages Modelini kullanmadan Product modeli üzerinden ProductImages modelini kolaylıkla güncelleyebiliriz.
örnek:
$create = new Product(array('name' => 'foo');
$create->save();
// oluşturulan Product Modeline görsel ekleyelim.
$create->addImage(array('/foo1.jpg','foo2.png')); // Product modeli üzerinden ProductImages Modeli güncelledik.
genel mantığı bu.
Pivot tablolar veritabanında olmayan bir tablonun birden fazla tabloyu birleştirerek oluşturulan sanal tablolardır. Geçici durumlarlar için veritabanı tasarımını bozmadan sanal tablolar üzerinde çalışmak için kullanılır.