Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with cakephp handling multiple related records.

Status
Not open for further replies.

petrosky

Technical User
Aug 1, 2001
512
AU
Hi there,

I am a relatively new user of the cakephp framework. I am creating an internal application for employees to be able to enter orders.

I currently use jquery to add rows to the "many" side of the order.

Eg. One order can have many products and notes.

So jquery is currently appending a table row in the following fashion.
Code:
<input type="text" name="data[Product][1][Quantity]" /> Description Price etc...
<input type="text" name="data[Product][2][Quantity]" /> Description Price etc...
Then when cake does the saveAll it creates a new order and then inserts the related products/notes.

I would like to have jquery actually dynamically add/delete these rows instead of just building the html form.

I am just lost with regards to how to configure the controller to handle these inserts. I'm thinking I need to "secure" an order number somehow to allow the products to be related but I really am just lost as to how the controller/view should work.

If anyone has any experience or better ideas I would love to hear them.

Thanks for any help anyone can provide.

Petrosky


Remember- It's nice to be important,
but it's important to be nice :)
 
Leaving aside cake thé normal way is to have a table for orders and a table for order line items.
So the order table might have columns for customerid orderid and orderdate and orderstatus.
The line item table would have the data on each line item and be associated with the order table via a foreign key.

No idea how cake works but this is a piece of cake to code outside of a framework.

As for the front end I'm confused. You say that jquery is adding rows as you need them. But then you say you want it to add rows dynamically. To my mind your description already has jquery acting dynamically. If you need more help on that you will need to be clearer and to post the rendered HTML/JavaScript.

For the php I cannot recommend using frameworks until you fully understand how vanilla php works and how your framework of choice limits php (they all do)
 
Hi jpadie,

Re-reading my OP I didn't make myself very clear. I thank you for your good advice as usual. I have been programming PHP for about 5 years but it is not my primary job function (Sys Admin/Support) so I have always been a "hacker".

My DB design is ok (I think)

I have tables for customers, orders, products, notes, suppliers all related as you would expect. It is working the way I am doing it now but it just seems clunky to me.

Here is an example of the "add" method of the controller should you wish to see the cake side of it...

Code:
	public function add() {
		if ($this->request->is('post')) {
			$this->Order->create();
			if ($this->Order->saveAll($this->request->data)) {
				$this->Session->setFlash(__('The order has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The order could not be saved. Please, try again.'));
			}
		}
		$employees = $this->Order->Employee->find('list');
		$customers = $this->Order->Customer->find('list');
		$suppliers = $this->Order->Supplier->find('list');
		$notes = $this->Order->Note->find('list');
		$this->set(compact('employees', 'customers', 'suppliers'));
	}

I will keep plugging away at it but thanks again for your reply.

Regards,

petrosky

Remember- It's nice to be important,
but it's important to be nice :)
 
i suspect you need to have a proper look in your order object. it looks to be these class methods that are doing the heavy lifting order->create() and order->saveAll(). I'd guess the latter is just a dbwrite method, so the createALL method would be my starting point for investigation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top