I want list all objects of my model, and write to file the id of selected model. Using SiteController I render my page but what model I shall use?
$models = myModel::model()->findAll();
$list = CHtml::listData($models, 'id', 'name');
echo CHtml::dropDownList( ???? , $select, $list);
If I get what you're trying to do, You're talking about two models. Like tbl_product => Product and tbl_category => Category.
For demonstration purpose: Say, you want to create a new product and every product must belong to a category, then you might make use of the active dropdown. Using code similar to yours, you can say:
$category = Category::model()->findAll();
$list = CHtml::listData($category, 'id', 'name');
An important thing to note is that CHtml::activeDropDownList() expects different kinds of arguments. The main difference between it and CHtml::dropDownList() is that activeDropDownList( is tied to a Model while dropDownList() isn't.
public static string activeDropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array ())
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ())
So, using the example, assuming our Product model has a field called category_id, then the dropdown list would be generated using either:
CHtml::activeDropDownList($model, 'category_id', $list);
or if you've created an Activeform object like this:
$form=$this->beginWidget('CActiveForm');
then you could create the dropDown list like this:
$form->dropDownList($model, 'category_id', $list);
Where $model would be the Product model.
I hope this has been helpful.
Related
Imagine the following Model:
class Model
...
field :things, type Hash
...
end
I would like then to be able to search for documents which has a specific attribute inside the things hash like so:
model = Model.where('things.order_id' => 9689689)
or
model = Model.where('things.order_id' => 9689689).all
But that doesn't find the document(s) I'm looking for. Neither does the following:
model = Model.elem_match(inside: {order_id: 116152})
nor this one:
model = Model.where(:inside.elem_match => {order_id: 116152})
What am I missing here?
Please note this is not an embedded document, but simply a model which contains hashes (though would be nice to find a way to do similar searches in embedded models).
Any ideas?
model = Model.where(:'things.order_id' => 9689689)
I have a rails app with 4 Model classes, with multiple instances in each table. I have created backbone Model and Collection classes in CoffeeScript to match. I can successfully load all of the collections and can render them in views. So far, so good. Here is one of my collections, with its associated model:
class window.CoffeeWater.Collections.Histories extends Backbone.Collection
url: '/api/histories'
model: History
class window.CoffeeWater.Models.History extends Backbone.Model
I need to be able to create a History model object, and then add it to the Histories collection. The documentation states that I have to set a 'collection' property when creating my new model, in order for it to get the 'url' property from the collection. My problem is that I can't seem to set the 'collection' property value correctly, because the url property does not get set on the model instance
attributes = {'start_time': new Date (1434740259016), 'stop_time': new Date (1434740259016 +(86400*1000)), 'valve_id': 2}
options = { collection: window.CoffeeWater.Collections.Histories }
history = new window.CoffeeWater.Models.History(attributes, options)
window.CoffeeWater.Objects.Collections.Histories.add(history)
Inspecting the resulting 'history' object does not show the same attributes that exist in models already in the collection, and the url property is missing.
I am currently at a loss. Does anyone have an example on how to do this? The backbone.js docs do not show any relevant examples.
The way the url in model is defined like this.
If there is a url property in model.
class window.CoffeeWater.Models.History extends Backbone.Model
url:'/api/history/1'
Then when model.fetch(), it will call that url. If this property is not found, it will see if the associated collection does have a 'url'. You try to set this collection variable.
What you are missing is this. This
class window.CoffeeWater.Collections.Histories extends Backbone.Collection
is actually a definition of a class. It is not an object yet. You need to initialize it, like you do in java. so
var historyCollection=new window.CoffeeWater.Collections.Histories()
Now you have a collection object. When you do
historyCollection.add(history);
The "collection" of model is automatically set to the "historyCollection" object, which contains a 'url'. So in fact, you do not need to manually put this in the option.
basically
attributes = {'start_time': new Date (1434740259016), 'stop_time': new Date (1434740259016 +(86400*1000)), 'valve_id': 2}
var historyCollection=new window.CoffeeWater.Collections.Histories()
var history = new window.CoffeeWater.Models.History(attributes)
historyCollection.add(history)
I am facing trouble using doctrine join. I can't share my code. But I can tell you scenario.
Please help me to achieve that.
I have created 2 entity. One User and Language.
User table is having foreign key language_id. and Language is master table with id and code fields.
I want to fetch user with some criteria, such a way it returns Language code from Language table as well.
I write join for that but it returns some full object...
Not sure how to fetch corresponding language code from Language table for language_id set in user table
If there is some example you know which can help me then also fine
i have return this in __construct()
$this->languageObj = new ArrayCollection();
when we print it is gives this
[languageObj:User:private] => Common\User\Entity\Language Object
(
[languageId:Language:private] => 1
[languageCode:Language:private] => en
[languageName:Language:private] => English
[languageCode2:Language:private] => User Object
RECURSION
)
I am not able to fetch languageCode from the object
You need methods defined in your entity to return the value from the object. It seems like everything is correct you would just need to grab the value from the entity. Here is an example:
$userEntity->getLanguageObj()->getLanguageId();
Your user Entity would need the getLanguageObj method which you can define like this:
public function getLanguageObj() {
return $this->languageObj;
}
And your Language Entity would also need a getLanguageId method:
public function getLanguageId() {
return $this->languageId;
}
Hope that helps!
Since Joomla 2.5 I can call functions from the default model now with this code:
$result = $this->get('Data');
Where get and Data together are leading to the function name "getData".
But I cannot do this:
$myModel = $this->getModel('special_model');
$result = $myModel->getData();
and I can also not do:
$myModel = $this->getModel('special_model');
$result = $myModel->getData('myId');
So, I can't call a method directly and also not with a parameter? Is it correct? Why is it like this? In J!1.5 this was possible.
Best Regards
Björn
What you have actually will work in J2.5, but you have to add one more piece. That should work as is if you are in the 'special_model' view. Based on the naming though, you are trying to add a second model to the view. This model has to be added to the view from the controller:
$view = $this->getView('myview', 'html') ;
$view->setModel( $this->getModel( 'special_model' )) ;
Again, add that to the controller, and the code you have will work in the model.
I'm exploring the possibility of using MVC for my next e-commerce site. One thing I can't seem to figure out is whether or not I can use the same URL convention I normally use. Currently, the URL for any product could be one of the following:
Category/SubCategory/Product1.html
Category/SubCategory/SubSubCategory/Product2.html
Category/SubCategory/SubSubCategory/Product3.html
Category/SubCategory/SubSubCategory/SubSubSubCategory/Product4.html
etc.
The issue I'm having is with the nested category structure. So far the only thing I've come up with is as follows:
routes.MapRoute(
"Products",
"{categories}/{productname}",
new { controller = "Product", action = "Details", productname = UrlParameter.Optional },
new { categories = #"\w+/\w+" }
);
I was hoping that {categories} could be matched with any one of the following which I could process to identify the right category that the product belongs to:
Sport/Tennis/Rackets/ProductA
Sport/Badminton/Rackets/ProductB
But the route shown above doesn't work correctly.
Does anyone know how this can be achieved, or if it can't be done?
The routing system allows you to define catchall parameters, which ignore slashes and capture
everything up to the end of a URL. Designate a parameter as being catchall by prefixing it with an
asterisk (*).
routes.MapRoute(null, "Articles/{*articlePath}",
new { controller = "Articles", action = "Show" }
);
You can only have one catchall parameter in a URL pattern, and it must be the last (i.e.,
rightmost) thing in the URL, since it captures the entire URL path from that point onward.
One Caveat though, it doesn’t capture anything from the query string as route objects only look at the
path portion of a URL.
Catchall parameters are useful if you’re letting visitors navigate through some kind of arbitrary
depth hierarchy, such as in a content management system (CMS).
You can use the RouteData object to extract information about the route. For your needs, you would probably create a custom route handler that parses the route data and calls the correct controller methods.
You need access to the individual segments of the URL so you need to divide the category segment into two segments. That would make it much easier.
Let's say we call Tennis and Badminton categories and Rackets within those categories as a product class
You need a way to access the category, productClass and productName parameters. Supposing that "Sport" is fixed in this case, I will do it like this:
routes.MapRoute(
"Products",
"sport/{category}/{productClass}/{productName}",
new { controller = "Product", action = "Details", productClass = UrlParameter.Optional, productName = UrlParameter.Optional }
);
Your action method will be something like this
public ActionResult Details(string category, string productClass, string productName){
//Do whatever you need to do in order to get the specified product
}
You could use Areas in MVC2
So it would read:
Area/Controller/View/id
So in your case it would end up being:
Sport being the area,
Tennis The controller,
Rackets the view,
ProductA being an ID or querystring,
http://www.asp.net/mvc/videos/aspnet-mvc-2-areas
Hope this makes sense.