Note that there are some explanatory texts on larger screens.

plurals
  1. POphp oo - how to do it right?
    primarykey
    data
    text
    <p>I have been creating a website using OO for the first time and I'm struggling to find the right way to go. I don't like classes where everything is done in one class so I separated the entity and database class. For example: <a href="http://w3cyberlearning.com/php/mysql_oop_product_class.php">http://w3cyberlearning.com/php/mysql_oop_product_class.php</a> I find this class not so logical. -why should a product contain a database instance -$product->get_products() insn't logical at all I think.</p> <p>So I create a separate class according to the following structure:</p> <pre><code>Table: Products Fields: productID,productName,categoryID Table: ProductCategory Fields: categoryID,category Classes: Product -productID -productName -category (object) DataProduct -insertProduct($product) insert query... -deleteProduct($product) delete query... -getProduct($id) select data from table ... (join between product and category to get all the fields) $category = new Category($categoryID,$categoryname) $product = new Product($productID,$productName); $product-&gt;setCategory($category); return $product; ProductCategory -categoryID -category DataProductCategory ... </code></pre> <p>But now I'm stuck with some questions wether I'm handling it right.</p> <h2>Question 1</h2> <p>To for example, delete a product, I can execute two different scripts, which one should I use?</p> <pre><code>http://www.mywebsite.com/books/1/ $id = 1 (1 retrieved from the url) $DataProduct = new DataProduct(); $DataProduct-&gt;deleteProduct($id); OR $product = new Product(); $product-&gt;setId($id); $DataProduct = new DataProduct(); $DataProduct-&gt;deleteProduct($product); </code></pre> <h2>Question 2</h2> <p>When I retrieve a product from the database, I create a new Product Object and I add the category as an object to the Product. Lets say you have an adminpage where you want to add a new product and you have an option list with categorieID ids:</p> <pre><code>&lt;input type="text" name="productname"&gt; &lt;select name="categories"&gt; &lt;option name="cat1" value="1"&gt; &lt;option name="cat2" value="2"&gt; &lt;/select&gt; </code></pre> <p>If I want to save this product to the database, what is the best way to do this? In the database, the productstable only holds the categoryID offcourse, as an foreign key to the categorytable.</p> <pre><code>$product = new Product(); $product-&gt;setProductName("test"); $product-&gt;setCategoryID(1); (and add a variable categoryID in the products class) $dataProduct = new DataProduct(); $dataProduct-&gt;insertProduct($product); </code></pre> <p>and just get the $product->categoryID and insert it into the database along with the $product->productName</p> <p>or should I do:</p> <pre><code>$product = new Product(); $product-&gt;setProductName("test"); $dataCategory = new DataCategory(); $dataProduct = new DataProduct(); $category = $dataCategory-&gt;getCategory(); (which retrieves the categoryID and name, creates the object and returns it) $product-&gt;setCategory($category); $dataProduct-&gt;insertProduct($product); </code></pre> <p>and with saving retrieve the id for the category from the object?</p> <p>Both ways will work, but I'm a little bit stuck on which is the way to go.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload