I am using a component for parsing a country api in yii. So in the form drop down list call the function for listing country. The function returned country list as array.
form.php
<?php echo $form->labelEx($model,'country'); ?>
<?php $cty= Country::getCountry();
echo $form->dropdownList($model,'country', $cty , array('style'=>'width: 175px','empty'=>array('empty'=>Yii::t('app','Select Country'))));?>
Now the country list loaded correctly in drop down, but when on saving time the corresponding id of country is saved. i want to save the country name in db.How it solved?
You have to build your own custom array with the needed keys/values, e.g. :
$cty = Country::getCountry();
$cty = array_combine(array_values($cty), $cty);
You can use this way (in case if you need all items of Country table)
$cty = CHtml::listData(Country::model()->findAll(), 'name', 'name');
Related
I created a customer attribute in backend magento, but I want to show this attribute to the user so that he can alter its value in the frontend. My input field is being displayed in the frontend, but the value is not there. I am not able to get this value. I found that the value that I need to display is in the apscustomer_entity_varchar table and the column is called 'value'. How can I get that value from that table? I was trying this:
$collection = Mage::getModel('eav/entity_attribute')->getCollection();
foreach ($collection as $data) {
return $data;
}
but it was not working, so I used SQL code and it worked. However I know that's not a nice way to do that in Magento. What I did was something like:
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT * FROM `apscustomer_entity_varchar ` WHERE `entity_id`='$id'";
$rows = $connection->fetchAll($sql);
How can I get the value column from my apscustomer_entity_varchar table in the magento way, using the getModel?
You need to retrieve the customer of the session and then you can get the attribute you want :
Mage::getSingleton('customer/session')->getData('attributeName');
Mage::getSingleton('customer/session')->getAttributeName(); //Magic getter
Where attributeName is the name of the attribute you want to get the value.
I found out how to do that :D
It's actually very simple. Even if this is a custom customer attribute (created in Magento admin panel), I need to get the attribute not using the 'eav/entity_attribute' model, but using the 'customer/customer' model. So I just need to do that:
$customer_data = Mage::getModel('customer/customer')->load($customer_id)->getData();
echo $customer_data['attributeThatYouWant'];
If you are not sure what is the name of your attribute you can look at Magento Admin Panel under Customer/Manage-Attributes, or you can use that after getting the model:
var_dump($customer_data);
Now you are able to get the name of all customer attributes.
Do I need some more work, to get a select box with the corresponding data (e.g. land list from an another db-table) in the created insert form (via CRUD) or it is enough to define the relations in the models and yii would do this for me automatically?
Since you haven't provided any code, let me show you with an example. Suppose we have a user table and a group table and need to select a group for a user which is selected with a select box.
In the user model you can have a function like
public function getGroupName()
{
return CHtml::listData(Group::model()->findAll();
}
In the form view of user create you can populate the select box like below:
<?php echo $form->dropDownListRow($User, 'group_id', $User->getGroupName(),array('prompt' => 'Select ...')); ?>
i'm madding a form (Yii framework) that does not represent a database table but it contains a part "mapped" to a database table (like a belong_to relation).
So you can consider a contact form in which you can choose to whom send the email with a dropdown list that represent users in database.
And below you can type your email subject and content.
So here the contact form is not stored in the database, so it is not an ActiveRecord instance, but it contains "relation" like an ActiveRecord has.
My question is : How do i build my form class ?
I want to be able to do this in the view :
$activeFormWidget->dropdown($form->user, 'name', User::model()->getUsers());
You can use the CHtml::dropdownList to build dropdown list without a CActiveForm:
<?php
echo CHtml::dropDownList('user', // the input name
'', // initial selected value
CHtml::listData(User::model()->getUsers(), 'id', 'name'), // your data
array()); // htmlOptions
?>
I have a case where user can select multiple values in the list box and save it to the database using model.
Here is the table structure
user_id int(11) , cars_id int(5)
Here is the snippet of my view
<?php echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20) );?>
<?php echo CHtml::dropDownList("targetCars", '', array(),array('size'=>20) );?>
User selects the cars from sourceCars and moves into targetCars using Jquery ( This part is done) and
clicks on Save or Submit button .
Now I should be able to save all the cars he/she selected in the targetCars list. Moreover in model I should put a condition that user can't save more than 10 cars and at least one car should be selected . Also user can select 5 cars at one time and next time when he comes he should be able to select max 5 cars only since he already save 10 records .
Could you please throw me some idea to implement this ? any Links that can guide me ?
your question is to limit selection of cars between 1-10.
You need validate user input both client and server.
At server,you can custom a ActiveRecord validation
public function rules()
{
return array(
array('cards_id', 'limitSelect','min'=>1,'max'=>10),
);
}
public function limitSelect($attribute,$params)
{
//and here your code to get the count of selection of cars for a user
...
if($count<=$params['min'])
$this->addError('cards_id','at least one car should be selected');
if($count>=$params['max'])
$this->addError('cards_id',' can't select more than 10 cars');
}
//and for mutiple select you can code this:
echo CHtml::dropDownList("sourceCars", '',CHtml::listData(MasterCars::model()->findAll(),'cars_code','car_name'),array('size'=>20,'multiple'=>true) );
//anyway you can implement it in several way
Sounds like you want to use scenarios, see docs here. You can dynamically set the scenario with CModel::setScenario based on the user flow.
I’m searching for a way to display on a member profile page, the number of contributions in some content types. Basically it has to display something like this:
Blog(10)
Articles(10)
Questions(19)
Comments(30)
Tips(3)
I’ve installed some different modules (like “user stats”) that I though could help me but haven’t been successful.
I’m wondering if it would be easiest just to hard-code it into my template file by starting taking the uid and just run some queries with the content types I want to display but I’m not sure on how to do that either.
Any help og suggestions would be very much appreciated.
Sincere
- Mestika
Edit:
I found a solution to do it manually with a query for each content type but I'm still very interested in a solution that's more elegant and smoother.
I use this code:
global $user;
$userid = $user->uid;
$blog_count = db_result(db_query("SELECT COUNT(0) AS num FROM {node} n where n.type = 'blog' AND status = 1 AND n.uid = {$userid}"));
If you are using the core Profile module, you could use something like below. It will show the nodes created by the user whose profile is being viewed. As an added benefit, it only needs to execute one custom database query.
Insert this snippet into template.php in your theme's folder and change "THEMENAME" to the name of your theme:
function THEMENAME_preprocess_user_profile(&$variables) {
// Information about user profile being viewed
$account = $variables['account'];
// Get info on all content types
$content_types = node_get_types('names');
// Get node counts for all content types for current user
$stats = array();
$node_counts = db_query('SELECT type, COUNT(type) AS num FROM {node} WHERE status = 1 AND uid = %d GROUP BY type', $account->uid);
while ($row = db_fetch_array($node_counts)) {
$stats[] = array(
'name' => $content_types[$row['type']],
'type' => $row['type'],
'num' => $row['num'],
);
}
$variables['node_stats'] = $stats;
}
Now, in user-profile.tpl.php can add something similar to:
// If user has created content, display stats
<?php if (count($node_stats) > 0): ?>
// For each content type, display a DIV with name and number of nodes
<?php foreach ($node_stats as $value): ?>
<div><?php print $value['name']; ?> (<?php print $value['num']; ?>)</div>
<?php endforeach; ?>
// Message to show for user that hasn't created any content
<?php else: ?>
<?php print $account->name; ?> has not created any content.
<?php endif; ?>
This is just a general idea of what you can do. You can also add restrictions to the content types you look for/display, check permissions for users to see these stats, use CSS to change the look of the stats, etc.
If you are using Content Profile, you could use THEMENAME_preprocess_node() and check that the node is a profile node before executing this code.
Given your simple requirement and the fact that you have the SQL statement in-hand, I'd say just use that. There's no reason to add yet another module to your site and impact it's performance for the sake of a single query.
That said, from a "separation of concerns" standpoint, you shouldn't just drop this SQL in your template. Instead, you should add its result to the list of available variables using a preprocess function in your template.php file, limiting its scope to where you need it so you're not running this database query on any pages but the appropriate profile page.