Hey guys,
My task is to build ORM without any framework. So far, I have fetched everything inside ProductModel.php, but some data are joined (example down below), and they come from 2 different tables. How can I archive, to store data that comes from cart to cart object / model, and data that comes from products to product object/model, without loosing their join relationship?
Please provide examples and explanation, or any other resource that can solve my problem. Thanks for help!
Like I said, right now I store everything inside ProductModel (PDO::Fetch Class)
class ProductModel
{
private int $productid;
private string $name;
private string $description;
private float $price;
private string $imagepath;
private int $quantity;
private int $totalprice;
private int $ordered;
}
My repository goes like this ( $tableName is cart, and $classname is ProductModel -> Above this code)
/**
* Get Cart Items from unique (id) customer
*
* @param int $customerId
* @return bool|array
*/
public function getCartItems(int $customerId): bool|array
{
$tableName = $this->setTableName();
$className = $this->setClassName();
$statement = $this->connection->prepare("SELECT cart.quantity, product.productid, product.name, product.imagepath, product.price FROM `$tableName` as cart LEFT JOIN product as product ON product.productid = cart.product_id WHERE cart.customer_id=:customerId AND cart.ordered=0;");
$statement->execute(['customerId' => $customerId]);
return $statement->fetchAll(PDO::FETCH_CLASS, $className);
}
What I want to archive now, is to store the data to relevant classes like this
Product Model (Store Data from Product Table)
class ProductModel
{
private int $productid;
private string $name;
private string $description;
private float $price;
private string $imagepath;
}
Cart Model (Store Data from Cart Table)
class CartModel
{
private int $quantity;
private int $totalprice;
private int $ordered;
}