Ajax Sort a multicolumn table?

2007-05-15 Thread johnvv

I've seen the tutorial on creating a drag and drop list with Cake &
Ajax but I am interested in creating a sortable table with clickable
columns to sort the table.  I do not need it to update the database in
any way.

Any tutorials, hints, tips, suggestions for this?
Thanks
John


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Best Practices - Associations & calling functions

2007-05-14 Thread johnvv

Well the strange thing is that "findAfter" is not being called in a
deeply nested association.  Also functions calls to the model do get
triggered but I was hoping that the model would contain data retrieved
from the database (instead of just an empty Model "shell").
Here is a good example to illustrate:

Post hasMany Comments
Comment hasOne User

class PostsController extends AppController {
 ...
   function view($id=NULL){
$this->Post->id = $id;
$this->Post->expects(array('PostImage',
'Comment','Comment.User'));
$this->Post->recursive = 2;
$this->set('post', $this->Post->read());

// here I would like to call a function that is associated
with the Users (either in the User Model or UsersController)
// afterFind never gets triggered for the User model (no idea
why...) but it is in view's print_r of the $post
// this calls the model function but the object does not have
any data that was pulled from the db

$this->Post->Comment->User->foo();  // Outputs 'hello my name
is: '

   // my noob idea would be that you would have to access it from
the array of Comments like:
   foreach ($this->Post->Comment as $comment){
$comment->User->foo();
   }
  // produces:
  //   Notice: Trying to get property of non-object
  //   Fatal error: Call to a member function callMe()
  }
} // end Post


class User extends AppModel {
...
  function afterFind($result){
print('This never gets called even though there is User data
in the Post's view output');
return $result;
  }
  function foo(){
print('Hello my name is: ' . $this->User->name);
  }
...
}

Thanks for any help with trying to grasp Cake's object creation &
accessing scope.
- John




On May 14, 12:22 am, "Jon Bennett" <[EMAIL PROTECTED]> wrote:
> > I'm wondering what the best practice is for calling a method from
> > inside one controller that happens to associate with another Model/
> > Controller.
>
> > For Instance, if I have a blog post that has many comments and I want
> > to run a method within the CommentsController to format/clean the
> > comment... how would I do this?
>
> > I doubt something like this would work
>
> > PostController {
> > ...
> > function view($id){
> > ..
> > $this->Post->Comment->cleanComment();
> > }
>
> > Thoughts?
>
> if it's something you'll want after getting any comments (or posts)
> you could create a model method and then use afterfind to format the
> data how you want, does wonders for cutting down on repetitive code.
>
> check out the models section about callbacks in the manual
>
> hth
>
> jb
>
> --
>
> jon bennett
> t: +44 (0) 1225 341 039 w:http://www.jben.net/
> iChat (AIM): jbendotnet Skype: jon-bennett


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Calculating an Average?

2007-05-13 Thread johnvv

Let's say the blog example has the following associations:
Post hasMany Comments
Comment hasOne User
User hasMany Reviews

Currently I have something like this (let me know if I'm doing
something n00b) :)

class PostsController extends AppController {
 ...
function view($id=NULL){

$this->Post->id = $id;
$this->Post->expects(array('PostImage', 'Comment','Comment.User'));
$this->Post->recursive = 2;
$this->set('post', $this->Post->read());
}

As you can see I haven't pulled up the user reviews yet but for the
purpose of viewing a Post, I just want to display the User's average
reivew.

How can I calculate the average rating for a user (using good Cake
practices)?

Thanks
John


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Best Practices - Associations & calling functions

2007-05-13 Thread johnvv

I'm wondering what the best practice is for calling a method from
inside one controller that happens to associate with another Model/
Controller.

For Instance, if I have a blog post that has many comments and I want
to run a method within the CommentsController to format/clean the
comment... how would I do this?

I doubt something like this would work

PostController {
...
function view($id){
..
$this->Post->Comment->cleanComment();
}

Thoughts?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



hasAndBelongsToMany in model with conditions?

2007-05-13 Thread johnvv

I've heard that it's good practice to put associations in the model
(rather than using bindModel) but when declairing a HABTM
relationship, I can't figure out how to add conditions or variables to
the relationship declairation.

For example,
class Product extends AppModel {
  var $hasAndBelongsToMany = array('Rebate' => array('className' =>
'Rebate', 'condition' => 'Rebate.expires >= ' . date('Y-m-d')));

}

Obviously the above doesn't work because you can't use a variable in
another variables declaration.
I suppose I could change it to 'Rebate.expires > NOW()' but this type
of question comes up often such as adding the condition that a model
has
"is_active = 1", or
"date > $start AND date < $end"
... or something similar.

So how can association relationships include conditionals or variables
if I want to keep the association in the model?
Thanks
John


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Associations to reduce # of queries

2007-05-09 Thread johnvv

Ok, I was able to get around the error messages with this:

// CategoryController
..
function view($id){
..
   loadModel('Product');
   $p = new Product();
   $this->Product = $p->getProductsByCategory($id, $start, $limit,
$sort);
   pr($this);
   $this->set('category', $this->Category->read());
}

Would show that the Category['Product'] object was populated with an
array of products.  For example
['Category']['Product'][0]['Product'][0]['product_name'] = 'Acme
XYZ123'
['Category']['Product'][1]['Product'][0]['product_name'] = 'Acme
MZ456'
However the view.thtml output did not show any recognition of a
Product object and I could not find a way to access it.  Do I need to
do a Save() call or something?
I didn't even get into trying to build the ProductImage model which
brings up an important question...

With 1 query, can I build 2 or more Models??

For instance, if I call a function in the Product model that joins the
product_image table, can I use the output to build both the Product
and ProductImage models?







On May 9, 12:19 pm, "Jon Bennett" <[EMAIL PROTECTED]> wrote:
> > Hmm... ok, I understand setting up the function in the Product model
> > to run the query but I'm confused about the CategoryController.
> > If I try to run:
>
> $catProds = $this->Category->Product->getCategoryProducts($categoryId);
>
> you have to traverse the associations tree back. If you're working
> within CategoriesController, then Product is an associated table to
> Category.
>
> try pr($this->Category); // pr is cake shorthand for print_r
>
> hth
>
> jon


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Associations to reduce # of queries

2007-05-09 Thread johnvv

Hmm... ok, I understand setting up the function in the Product model
to run the query but I'm confused about the CategoryController.
If I try to run:

$catProds = $this->Product->getCategoryProducts($categoryId);

I get "Undefined property: CategoryController::$Product "

or if I try
$catProds = $this->Product->getCategoryProducts($id);
I get "Undefined property: Category::$Product"

I also tried:

loadModel('Product');
$p = new Product();
$this->set('Products', $this->$p->getProductsByCategory($id));

yeilds the error:
Object of class Product could not be converted to string


So how I can I access the Product model to return an array of Products
and add them to the CategoryController?



On May 9, 3:47 am, soytuny <[EMAIL PROTECTED]> wrote:
> I don't think you will have much luck getting cake to produce a
> similar query.
>
> But an easy solution is to wrap your query inside a function in your
> Product model.  Say
>
> function getCategoryProducts($categoryId)
> {
>   return $this->query(/*that query you've written*/);
>
> }
>
> Then you can use that in your controllers:
>
> $catProds = $this->Product->getCategoryProducts($categoryId);
>
> Just be sure to use table aliases like "FROM products AS Product" to
> make the array nicer.
>
> Russell Austin
>
> On May 8, 3:04 pm, johnvv <[EMAIL PROTECTED]> wrote:
>
> > I'm to new using cake so file this question under 'newbies'...
>
> > I have the following models:
> > - Product (contains reference to manufacturer_id... and other
> > information)
> > - ProductImage (id,product_id,type [thumbnail,medium,large],
> > image_url)
> > - Manufacturer
> > - Category
> > - CategoryProduct (category_id, product_id)
>
> > Each Category has many Products
> > Each Product has 3 ProductImages (thumbnail, medium, large) - maybe
> > could be 3 separate "hasOne" associations with conditions for each
> > type
> > Each Product has 0 or 1 Manufacturer
>
> > I'd like to minimize the number of queries when viewing the products
> > in a category.
> > I've tried many different associations but each time cake has
> > generated separate queries to obtain the images and manufacturers.
>
> > Without cake, this can be accomplished with 1 query.  It would look
> > something like this:
> > "SELECT product.*,product_image.*,manufacturer.*
> > FROM category_product
> > INNER JOIN category ON category.category_id =
> > category_product.category_id
> > INNER JOIN product ON products.product_id =
> > category_product.product_id
> > INNER JOIN product_image ON product_image.product_id =
> > product.product_id AND product_image.type = 'thumbnail'
> > LEFT JOIN manufacturer ON manufacturer.manufacturer_id =
> > product.manufacturer_id
> > WHERE category_product.category_id = $id
> > ORDER BY $sort
> > LIMIT $start, $limit
>
> > But everything I have tried in cake has resulted in 40-some queries to
> > accomplish the same result.
> > Any ideas?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Associations to reduce # of queries

2007-05-08 Thread johnvv

I'm to new using cake so file this question under 'newbies'...

I have the following models:
- Product (contains reference to manufacturer_id... and other
information)
- ProductImage (id,product_id,type [thumbnail,medium,large],
image_url)
- Manufacturer
- Category
- CategoryProduct (category_id, product_id)

Each Category has many Products
Each Product has 3 ProductImages (thumbnail, medium, large) - maybe
could be 3 separate "hasOne" associations with conditions for each
type
Each Product has 0 or 1 Manufacturer

I'd like to minimize the number of queries when viewing the products
in a category.
I've tried many different associations but each time cake has
generated separate queries to obtain the images and manufacturers.

Without cake, this can be accomplished with 1 query.  It would look
something like this:
"SELECT product.*,product_image.*,manufacturer.*
FROM category_product
INNER JOIN category ON category.category_id =
category_product.category_id
INNER JOIN product ON products.product_id =
category_product.product_id
INNER JOIN product_image ON product_image.product_id =
product.product_id AND product_image.type = 'thumbnail'
LEFT JOIN manufacturer ON manufacturer.manufacturer_id =
product.manufacturer_id
WHERE category_product.category_id = $id
ORDER BY $sort
LIMIT $start, $limit

But everything I have tried in cake has resulted in 40-some queries to
accomplish the same result.
Any ideas?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Compound Keys in CakePHP

2006-09-25 Thread johnvv

I'm just getting started with CakePHP and would like to know if it can
handle compound keys in a database.
For example, if you were to add tags to a blog, the schema would
roughly be
table: article(article_id, article_text, PRIMARY_KEY(article_id));
table: tag (tag_id, tag_word, PRIMARY_KEY(tag_id));

Now if you want to associate tags with an article, there would be
another table:
article_tag(article_id, tag_id, PRIMARY_KEY(article_id, tag_id));

This seems like valid database design to me (feel free to argue or
present an alternative).  Since I haven't worked with CakePHP yet, I'll
ask: "Would CakePHP be able to work with this type of schema?"
And is it pretty straight forward?
- John


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~--~~~~--~~--~--~---