Mycart is stored in sessions which I wanna display in cgridview . I have used cgridview with dataprovider but can i do so with session value.Is it possible ?if not what would be the best way out to achieve so .If yes as I have no idea how do i proceed .
Please provide some guidance or examples.Do let me know if I'm not clear I'll make sure to clarify myself.
You could always create your own HTML table if you don't want to use data provider.
Yii CGridView require a data provider, therefore you can't use CGridView if you can't supply the data provider. Use CArrayDataProvider instead:
$sessionData = Yii::app()->user->getState('cart');
// $dataProvider = new CArrayDataProvider($sessionData); //throws undefined index:id exception
$dataProvider = new CArrayDataProvider($sessionData, array('keyField'=>'id'));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'your-grid',
'dataProvider' => $dataProvider,
'columns' => array(
'item_name',
'qty',
// etc
)
));
Related
I'm using Yii2 framework with advanced template.
I get problem with column alias in my controller file, here's my code:
$models = new ActiveDataProvider([
'query' => User::find()->select(['member'=>'fullname'])
]);
The above query equivalent with:
SELECT fullname AS member FROM User;
I send the data to the view using this code:
return $this->render('view', [
'model' => $models,
]);
I want to call the data in my view using GridView widget, here's my code:
echo GridView::widget([
'dataProvider' => $model,
'columns' => [
'member',
],
]);
However, I got an error that tell me the 'member' parameter is not defined.
How can I show the data of my query by calling the column name? (in my case it using alias)
I really appreciate any kind of helps!!
You should simply declare this attribute in your model :
class User extends ActiveRecord
{
public $member;
Read more : https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#selecting-extra-fields
ActiveDataProvider works only with model attributes. member obviously is not presented there.
First of all, maybe it's better to refactor column names to be more clear instead of writing aliases? I don't see any benefit in your example.
If you nevertheless need to use aliases, as alternative for adding additional properties to class, you can work with them with help of ArrayDataProvider and SqlDataProvider.
Examples of usage:
ArrayDataProvider:
use yii\data\ArrayDataProvider;
$dataProvider = new ArrayDataProvider([
'allModels' => User::find()->select(['member' => 'fullname'])->all(),
]);
SqlDataProvider:
use yii\data\SqlDataProvider;
use yii\db\Query;
...
$query = (new Query())
->select(['member' => 'fullname'])
->from('users');
$command = $query->createCommand();
$dataProvider = new SqlDataProvider([
'sql' => $command->sql,
'params' => $command->params,
'totalCount' => $query->count(),
]);
For more details and features of usage please see official docs.
For your case it's better to use ArrayDataProvider, SqlDataProvider is for more complex queries.
In case of one alias and using model methods adding additional attribute as suggested by soju can be better.
But in my opinion it's useless and it's better to refactor column names in case of some ambiguity.
how can I increment a value?
$app->db->update('videos', array(
'views' => 'views + 1'
), array(
'id' => $id
));
It doesn't work in many ways I tried.
A better approach could be to use the method executeUpdate()
$app->db->executeUpdate("UPDATE videos SET views=views+1 WHERE id=?", array($id));
Edit 2022
The API is working but marked deprecated. Use executeStatement() instead of executeUpdate()
You have to ask the db for the current value of views first, then increment the value and save the new value into db.
$result = $app->db->fetchAssoc('SELECT views FROM videos WHERE id = ?', array($id));
$result['views']++;
$app->db->update('videos', array('views' => $result['views']), array('id' => $id));
I'm saving data sent from a form.
In the Controller I am doing :
$this->User->create();
$this->User->save($this->request->data)
The $this->request->data looks like this:
'User' => array(
'password' => '*****',
'username' => 'ddddd',
'role' => '256/aa01bdf80d42beb48dd3225acddf447fdd7a39f3',
'parent_id' => '0/b6ba9bd57f6c70cf738891d4c6fac22abed4161d'
)
There are validation rules that works on 'role' and 'parent_id' to insure the role/parent ids are among those the user can access.
The validation changes the field values if the data is valid.
I also have a Tree behavior that is setting some tree fields in a beforeSave() filter in the behavior.
The validation rule is writing the change to $this->data->[$model][$field] as shown below.
public function checkListHash($check, $field) {
$explodedCheck = explode('/', $check[$field]);
if ($this->secureId($explodedCheck[0], $explodedCheck[1])) {
$this->data['User'][$field] = $explodedCheck[0];
return true;
}
return false;
}
The beforeFilter() in the behavior is changing the data array with statements like this:
$Model->data[$Model->alias][$ancestors] = $ancestorList;
When validation and the beforeFilter() processing is complete, I have a beautiful and correct array of data at $this->User->data that looks like this:
'User' => array(
'password' => '*****',
'active' => '0',
'role' => '256',
'parent_id' => '0',
'node' => '0',
'username' => 'ddddd',
'modified' => '2013-09-15 09:55:02',
'created' => '2013-09-15 09:55:02',
'ancestor_list' => ',0,'
)
However, $this->request->data is unchanged. And that is what is being save.
Clearly I'm not understanding the relationship of these various ways to get to the data. I've tried a variety of ways to address the data in the three contexts:
Controller
Model
Behavior
And I've tried $this->User->create($this->request->data); before the Controller save() statement.
In the controller, what I'm seeing as available data arrays:
PRIOR TO THE SAVE
$this->request->data = $this->data = proper data from the form
$this->User->data = some default, unpopulated array
PRIOR TO THE SAVE when I use $this->User->create($this->request->data)
all three arrays contain raw form data
AFTER THE SAVE in either case
$this->request->data = $this->data = exactly as before
$this->User->data = the properly massaged data
Can anyone sort me out?
Don Drake
Just to explain the data arrays to you, when you submit the form, the data from it is stored in $this->request->data on the controller. You are then modifying $this->User->data from inside the model, which is a different array on the model itself. It would not affect $this->request->data because it's a completely different array which belongs to the controller, and the model has no knowledge of it.
You are then saving the User model using the request data, which remains unchanged from when the form was submitted. This is logical and normal behaviour because you're not actually using the $this->User->data array that you've modified.
Your save might always be failing because the data the model is trying to save isn't the updated data, it's just the basic data from $this->request->data.
Try this:
$this->User->set($this->request->data);
$this->User->save();
Also, if you are using a beforeSave in your model, make sure the method returns true, or it will never actually go on to save.
I create my custom attribute, but when I go show this with my method it doesn't work!
See was do... create my attribute..
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$config = array(
'position' => 1,
'required'=> 0,
'label' => 'Height',
'type' => 'int',
'input' => 'text',
'apply_to' => 'simple,bundle,grouped,configurable'
);
$setup->addAttribute('catalog_product', 'height' , $config);
and I get a list of items in checkout...
$items = Mage::getModel('checkout/cart')->getQuote()->getAllVisibleItems();
foreach($items as $item){
echo $item->getSku() .'<br/>'; //just test... and all right!
echo $item->getHeight() .'<br/>'; //return empty! or....
echo $item->getData('height') .'<br/>';//return empty!
}
I set values in this attribute's fiels in my backend.
Thanks for help me!
Your attributes probably aren't being loaded by default. A cheat, without fixing the root problem of adding height to the collection's addAttributeToSelect() statement would be to load the product model again:
$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
echo $product->getHeight();
This doesn't solve the root of the problem though, and fires off an additional database query.
I asked a similar question a couple of months ago regarding loading additional data which contains some more information, although more related to collection loading than individual models: Retrieving additional data from already loaded Magento models.
Try echo $item->getProduct()->getHeight();
I'd like to add some new attributes to the admin users in Magento. These users are different than customers (just to make it clear) and it's only possible to set their user/name/lastname/mail/pass, but I'd like to add a few new attributes.
To do so, I guess I can use the addattribute function, but I need to find out which is the id of these admin users. For example, if I want to add a new attribute to a customer, I can use a function like this:
$setup->addAttribute('customer','attribute_id', $attr );
So, in this case, 'customer' is the id for customers. How can I find out which id is used for admin users? (this question can be extended to "How can I find the different id for the different types of attributes in Magento?").
==There is a chance that there is no way to change this. I've taken a look at the admin_user table and it's quite simple, all fields are there. So maybe there are no attributes in this case.==
Thanks
You can find all such ids (entity ids) in the eav_entity_type table.
And yes, there is no record for admin user. Because all data about admin users are stored in flat tables but not in eav. So to add a new attribute to admin user, you need to add a new column in the admin_user table
You will need to add a column to the admin_user table.
$installer->getConnection()->addColumn($installer->getTable('admin/user'), 'location', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 256,
'nullable' => true,
'default' => null
));
Then, if you want to add/edit this field from the backend you need to rewrite the method Mage_Adminhtml_Block_Permissions_User_Edit_Tab_Main::_prepareForm and add a new element in there:
$fieldset->addField('location', 'select', array(
'name' => 'is_active',
'label' => Mage::helper('adminhtml')->__('location'),
'id' => 'is_active',
'title' => Mage::helper('adminhtml')->__('location'),
'class' => 'input-select',
'style' => 'width: 80px',
'options' => array('1' => Mage::helper('adminhtml')->__('Yes'), '0' => Mage::helper('adminhtml')->__('No')),
));
Clear the cache and it should work.
No option till 1.7
thats what i use in the template to show the sku to an specific user bit dirty but works fine:
<?php
//EGS SKU added for Power User
$_powerUser = 777;
if (Mage::getSingleton('customer/session')->getCustomer()->getId() == $_powerUser)
{
echo '<div class="price-from">' . $_product->getSku() . '</div>';
}
?>