File: /home/artinside/www/raquel/source/Models/Ecommerce/Product.php
<?php
namespace Source\Models\Ecommerce;
use Source\Core\Model;
use Source\Models\Category;
/**
* Class Product
* @package Source\Models\Ecommerce
*/
class Product extends Model
{
/**
* Post constructor.
*/
public function __construct()
{
parent::__construct("products", ["id"], ["title", "uri", "content", "category"]);
}
/**
* @param null|string $terms
* @param null|string $params
* @param string $columns
* @return mixed|Model
*/
public function findProduct(?string $terms = null, ?string $params = null, string $columns = "*")
{
$terms = "status = :status AND post_at <= NOW()" . ($terms ? " AND {$terms}" : "");
$params = "status=post" . ($params ? "&{$params}" : "");
return parent::find($terms, $params, $columns);
}
/**
* @param string $uri
* @param string $columns
* @return null|Post
*/
public function findByUri(string $uri, string $columns = "*"): ?Product
{
$find = $this->find("uri = :uri AND status = 'post' AND post_at <= NOW()", "uri={$uri}", $columns);
return $find->fetch();
}
/**
* @param string $uri
* @param string $columns
* @return null|Post
*/
public function findByGlink(string $gLink, string $columns = "*"): ?Product
{
$find = $this->find("gallery_link = :gLink", "gLink={$gLink}", $columns);
return $find->fetch();
}
/**
* @return null|Category
*/
public function category(): ?Category
{
if ($this->category) {
return (new Category())->findById($this->category);
}
return null;
}
/**
* @return null|ProductVariations
*/
public function variations(): ?ProductVariations
{
if ($this->gallery_link) {
return (new ProductVariations())->find("product_id = :gl", "gl={$this->gallery_link}")->fetch();
}
return null;
}
/**
* @return bool
*/
public function save(): bool
{
$checkUri = (new Product())->find("uri = :uri AND id != :id", "uri={$this->uri}&id={$this->id}");
if ($checkUri->count()) {
$this->uri = "{$this->uri}-{$this->lastId()}";
}
return parent::save();
}
}