CakePHP database how it works? [closed] - cakephp

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have 3 tables: users, images (foreign key to user_id), comments (foreign keys: user_id and image_id)
Can somebody explain me or send me link to good tutorial how to work with database?
I mean e.g.:
1. I wants to take all Images which have any comment in Comments table. In SQL: select i from Images i, Commets c where i.id=c.image_id???
Can You explain me how can I access from one controller to another Model?
I was trying in Images controller:
$data = $this->Image->query('Select * from Images i, Comments c where i.id=c.image_id');
And in view: $image['id'] but I have error:Undefined index: id [APP\views\images\commented.ctp,
SO it does not work:/
How can I delete Image and all its comments? I used Zend and I get used to it and I have no idea how to do the same thinks in cakePHP :/
Can somebody explain me how to work with database in this cakePHP I know there are find, findAll etc functions but all of them are from a controller level ex. $this->Image->find() etc...
I need better tutorial than the basic cakePHP cookbook :/. I am open for any sugestions.
Regards,

You reference models in the controller in one of many ways. The two common ways are with the uses variable at the top of the controller:
var $uses = array('User','Image');
Or you can put it inline in your functions:
$this->loadModel('Image');
$this->Image->find('all'); ...
In addition, if you have the models linked by a foriegn key, you can even call it like:
$this->User->Image->find('all');
Once you get the hang of how CakePHP structures things, it makes it very clear.
So for your sql statement in the question, you could do something like the following from the users controller:
class UsersController extends AppController {
var $name = 'Users';
var $uses = array('User','Image','Comments');
function {my_function_name}() {
$this->Images->recursive = 1;
$images = $this->Image->find('all');
}
}
Now, as long as your relationships are correct in the model, the recursive function will build the images array with all of the comments attached to it.
array(
[Image] => array(
image data,
[comments] => array(
[0] comment data,
[1] comment data,
[2] ...
)
)
)
I hope that at least pushes you in the right direction. Happy coding!

Related

How to migrate from Yii to Woocommerce Wp, Still keep to old database (table,rows) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have an e-commerce website writen by yii framework. I want to rebuild with woocommerce on wordpress. And I want to keep old database (all table, rows). But wordpress save data on a different way (metadata).
what is solution in this case, how to import old database and custom field same my old database?
P/s: thanks for advance and sorry about my english.
Unfortunately, there is no easy way to do this:
First, you must create the post using wp_insert_post:
wp_insert_post( $post, $wp_error );
http://codex.wordpress.org/Function_Reference/wp_insert_post
Second, you must add the appropriate metadata using add_post_meta:
add_post_meta($post_id, $meta_key, $meta_value, $unique);
http://codex.wordpress.org/Function_Reference/add_post_meta
What you end up with is something like this psuedo-code:
$yii_products = (get products from yii db. Perhaps init a new PDO connection to connect to that db.)
foreach($yii_products as $product)
{
//Create post array from existing datatype in yii
$post = array(
'title' => $product->title,
...
);
//Save post
$post_id = wp_insert_post($post);
//Add metadata
//some_yii_field_name could be 'color', 'size' or whatever woocommerce uses
add_post_meta($post_id, 'some_yii_field_name', $product->some_yii_field_name);
}
This will create all of your new post types and with the appropriate metadata. I would put this code into a throw-away plugin used only for migration.
Good luck!

elements or view extensions or something else? reference app?

In cakephp [2.2] I have baked everything and my "people" view is quite busy with relations and phones and addresses and other related data. I do want all of that information visible in the people view, though not quite in the baked layout.
How should I handle those portions of the related data? I'm not sure if I should use elements or extended views or plugins or what, I'm kinda new to this and the documentation wasn't clear to me (at my level) which should be used when. The baked code seemed to be a monolithic approach, so I didn't get much help looking there.
Once the user chooses to edit a phone number (for instance) from the listing on the person view, it takes them to the phone edit view and then returns them to the phone listing (index view) and not the person view that they were on. How do I get them back to the person view instead?
The blog example they provide is nice, but is there a "reference" application somewhere for cakephp that demonstrates best practices on a wide variety of their features? I couldn't find one, or anything more than just a simple app example.
Thanks, I appreciate the guidance.
This is a rather broad question, but I'm going to try and answer it. I'm not sure how advanced you're programming knowledge is, so forgive me if I'm rehashing things you already know. First, this article was a great help when I started to use the framework for the first time as it explains what code should go where and why. It's the closest I've seen to a "reference application", which would actually be a great learning tool. You could try and have a look at some of the higher profile Cake applications, like Croogo (a Cake-based CMS). But the codebase is bound to be a little bit complex.
Personally I would use elements when you want to actually reuse them in different views. The problem however, is feeding the element its data. There's a method called requestAction, but even the manual states that this should be used with moderation and in combination with caching. The problem is that using a lot of requestAction calls in different elements litters your Controllers with methods and doesn't adhere to the "Skinny Controllers, Fat Models" mantra.
I would put most of the related data calls in their respective Models and call those Model methods from the Controller and feed them to the View. So let's say you want the 10 latest PhoneNumbers and related Users.
You would have a method in your PhoneNumber model which returns an array of users and their phonenumbers. Use the Containable behaviour to limit the number of related models which are returned. The code below is an example, so the practical implementation might vary:
public function getRecentPhoneNumbers($limit=10) {
$phoneNumbers = array();
$phoneNumbers = $this->find('all', array(
'limit' => $limit,
'contain' => array('User'),
'order' => 'PhoneNumber.id DESC'
));
return $phoneNumbers;
}
If the PhoneNumber and User model are properly related you would be able to call getRecentPhoneNumbers() from the User model:
$this->PhoneNumber->getRecentPhoneNumbers(10)
Or from the Users Controller:
$this->User->PhoneNumber->getRecentPhoneNumbers(10)
Say you have an element which shows a list of those 10 numbers and it accepts a variable called $recentPhonenumbers, you then set the variable in the relevant UsersController method with the returned array from the getRecentPhoneNumbers call:
$this->set('recentPhonenumbers', $this->User->PhoneNumber->getRecentPhoneNumbers(10));
This will make it available to the View that contains the element.
The extended views are relatively new (from Cake 2.1 and onwards) and I haven't used them, but seem a great way to create conditional markup.
As for the second question, redirecting the user to the person view, rather than the index view. This is a matter of adjusting the redirect (see the manual for more details) in the edit() method of the Controller. Standard baked edit() methods accept an $id parameter you can use this to redirect to the view() (which probably also accepts an $id paramater).
So the redirect probably looks something like this:
$this->redirect(array('controller' => 'users', 'action' => 'index'));
Change it to:
$this->redirect(array('controller' => 'users', 'action' => 'view', $id));

how to use database in Scala, Lift? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am able to successfully read the data from my form and using println show them in console,
I need to add them to the database, currently I am using the predefine database for my authorisation stuffs, please help me to add the form values to the database. So far I have created the following class.
object Form extends Form with LongKeyedMetaMapper[Form] {
override def fieldOrder = List(name,age)
}
class Form extends LongKeyedMapper[Form] with IdPK {
def getSingleton = Form
object name extends MappedString(this,10)
object age extends MappedString(this,5)
}
There exists an example for setting up Lift Mapper at the Lift wiki-site. See https://www.assembla.com/spaces/liftweb/wiki/Mapper

CakePHP 2.0.x: ACL: Multiple Groups?

I know this question has been asked before, but I don't know if it's been asked specifically about CakePHP 2.0.x. I haven't been able to find any information about whether or not having users be a part of multiple groups is now possible with the ACL component. I never used the ACL component with CakePHP 1.3.x because it confused me. If it's better now, though, I'd like to use it so I don't re-invent the wheel by rolling my own. Any help would be appreciated.
I haven't tested it or used it like this before, but I can imagine one way:
Have a User model and a Group model, and User HATBM Group as Shaz Amjad notes.
At the point you're doing your access control, fetch a list of all Groups that User belongs to (probably using bindModel)..
Then, something like:
$permits = array();
foreach ($thisUsersGroups as $group) {
$permits[] = $this->Acl->check($group, 'myclass', 'update')
}
If $permits contains at least one true, they should be permitted.
There might well be a better or more automagic way of doing it, but I don't see what that shouldn't work in principle.

Get data from another table W/O associations [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm using CakePHP, and trying to pull data from a table outside my current controller. The only thing I can find online is about using associations and belongsTo and such, however these two items shouldn't be related. I simply want to pull data from another table at will.
You could also use the loadModel inside your functions.
$this->loadModel('User', 2);
$user = $this->User->read();
Well if you want to access different model (not the related to the controller) you can use
class SomeController extends AppController {
var $uses = array('Model1', 'Model2');
// and then later in the code you can use them like this
function index() {
$this->Model1->doStuff;
$this->Model2->doStuff;
}
}
you can use
$user=App::Model('User');
$user->find('all');
either
//only controller
$this->loadModel('MyModel');
$res = $this->MyModel->find(...)
or
//everywhere
$this->MyModel = ClassRegistry::init('MyModel');
$res = $this->MyModel->find(...)

Resources