| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Models\WpModels; |
| 4 |
|
| 5 |
use FluentCart\App\Models\Model; |
| 6 |
use FluentCart\App\Models\Concerns\CanSearch; |
| 7 |
use FluentCart\App\Models\Product; |
| 8 |
|
| 9 |
class Term extends Model |
| 10 |
{ |
| 11 |
use CanSearch; |
| 12 |
|
| 13 |
protected $table = 'terms'; |
| 14 |
|
| 15 |
/** |
| 16 |
* The primary key for the model. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
protected $primaryKey = 'term_id'; |
| 21 |
|
| 22 |
public function taxonomy(): \FluentCart\Framework\Database\Orm\Relations\HasOne |
| 23 |
{ |
| 24 |
return $this->hasOne(TermTaxonomy::class, 'term_id', 'term_id'); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get the taxonomies for the term |
| 29 |
*/ |
| 30 |
public function taxonomies() |
| 31 |
{ |
| 32 |
return $this->hasMany(TermTaxonomy::class, 'term_id', 'term_id'); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Get products associated with this term |
| 38 |
*/ |
| 39 |
public function products() |
| 40 |
{ |
| 41 |
return $this->hasManyThrough( |
| 42 |
Product::class, |
| 43 |
TermRelationship::class, |
| 44 |
'term_taxonomy_id', // Foreign key on term_relationships |
| 45 |
'ID', // Foreign key on products |
| 46 |
'term_id', // Local key on terms |
| 47 |
'object_id' // Local key on term_relationships |
| 48 |
)->join('term_taxonomy', 'term_relationships.term_taxonomy_id', '=', 'term_taxonomy.term_taxonomy_id') |
| 49 |
->where('term_taxonomy.term_id', $this->term_id); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|