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(...)
Related
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!
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
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I use Backbone & Marionette for my webapp,
I Have a collection of tracks, when a track is added and when the player don't read any track, I want put "played" style to itemView, or "paused" to the track added on the cue.
So I want to add a class for each new Backbone model.
When I add a model, the class is added to the previous element, but not on the newest.
This is my CompositeView :
var trackListYTView = Marionette.CompositeView.extend({
template: "#playlist",
id: "trackList",
itemView: trackView,
itemViewContainer: "tbody",
initialize: function(){
this.bindTo(this.collection, "add", this.modelAdded);
},
modelAdded: function(model){
if(status) this.$('#status').addClass('icon-pause');
else this.$('#status').addClass('icon-play');
}
I think "this" is not the good selector for the new itemView.
Thanks for helps, Luca
I think your problem is the selector - #status. The element id should be unique. You should not have multiple elements with same id. Use a class selector, for example '.statusClass', instead of '#status'.
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.
Is it possible to set values in $this->data which is received in controller from the view.
We can use if($this->data['id']) but why we cant set values in it?
Will this work$this->data['id']=$userid;
You have a couple of options
The first is to just create your own variable and modify that:
$myData = $this->data;
$myData['id'] = $userid;
$this->Post->save($myData);
The second is to use $this->request->data. It contains the same info as $this->data but can be modified.
$this->request->data['id'] = $userid;
$this->Post->save($this->request->data);
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!