I am scratching my head to figure out why this legacy code supposedly will reset the indexes on a database table. Can you shed some light on it?
$employee = new Employee();
$employee->update([
'recruited' => 0,
'job_id' => null,
'job_start_date' => null,
'job_end_date' => null,
'interviewed' => 0
]);
There are a corresponding Eloquent model called Employee.
try to use :
DB::table('employees')->update([
'recruited' => 0,
'job_id' => null,
'job_start_date' => null,
'job_end_date' => null,
'interviewed' => 0
]);
Related
I user this code for insert data .
$conv = DB::table('conversations')
->insert([
'is_seen' => $other_user_id,
'user_one' => $user_id,
'user_two' => $other_user_id,
'user_one_status' => 1,
'user_two_status' => 0,
'message_status' => 0,
'last_message' => $messageCon
]);
Its return true false value. I need last inserted row.
Assuming there's a reason you're not using Eloquent, you could use insertGetId.
$conv = DB::table('conversations')
->insertGetId([
'is_seen' => $other_user_id,
'user_one' => $user_id,
'user_two' => $other_user_id,
'user_one_status' => 1,
'user_two_status' => 0,
'message_status' => 0,
'last_message' => $messageCon
]);
Caveats from the documentation:
Auto-Incrementing IDs
If the table has an auto-incrementing id, use the insertGetId method to insert a record and then retrieve the ID:
When using PostgreSQL the insertGetId method expects the auto-incrementing column to be named id. If you would like to retrieve the ID from a different "sequence", you may pass the column name as the second parameter to the insertGetId method.
Insert cant return whole row.
You can change it to insertGetId and then select it from database by that id.
Or use models, and use create method (Conversation::create([...]))
In my TCA I use a select to an other table.
'modules' => [
'label' => 'LLL:EXT:myextension_module_table/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_semester.modules',
'config' => [
'type' => 'select',
'renderType' => 'selectMultipleSideBySide',
'enableMultiSelectFilterTextfield' => true,
'foreign_table' => 'tx_myextension_domain_model_module',
'minitems' => 1,
'maxitems' => 99,
],
],
Under "Selected Items" I can sort the items. Now I want to use this sort order in fluid. In database I see the right order 21,1,2,3,4,28. But in fluid the items are always sorted by uid.
Adding 'sortby' => 'sorting', with all needed changes doesn't solve my problem. In this case I can order records in list view. But I don't want this order. I want to show order from "Selected Items" in frontend.
In my ModuletableController.php I get the selected moduletable from Flexform.
/**
* action show
*
* #param \Vendor\Myextension\Domain\Model\Moduletable $moduletable
* #return void
*/
public function showAction(\Vendor\Myextension\Domain\Model\Moduletable $moduletable = NULL) {
if (is_null($moduletable)) {
$moduletable = $this->moduletableRepository->findByUid($this->settings['singleModuleTable']);
}
$this->view->assign('moduletable', $moduletable);
}
An in the template Show.html I loop through it.
<html xmlns:f="http://typo3.org/ns/TYPO3/Fluid/ViewHelpers">
<f:layout name="Default" />
<f:section name="main">
<h2>{moduletable.title}</h2>
<f:for each="{moduletable.semester}" as="semester">
<p>{semester.title}</p>
<f:for each="{semester.modules}" as="module">
<p{module.title}</p>
</f:for>
</f:for>
</f:section>
Solution is to use MM tables. Then sort order is set in TCA select field.
In TCA:
'modules' => [
'label' => 'LLL:EXT:myextension/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_semester.modules',
'config' => [
'type' => 'select',
'renderType' => 'selectMultipleSideBySide',
'enableMultiSelectFilterTextfield' => true,
'foreign_table' => 'tx_myextension_domain_model_module',
'foreign_sortby' => 'sorting',
'MM' => 'tx_myextension_semester_module_mm',
'minitems' => 1,
'maxitems' => 99,
],
],
In ext_tables.sql
CREATE TABLE tx_myextension_semester_module_mm (
uid_local int(11) unsigned DEFAULT '0' NOT NULL,
uid_foreign int(11) unsigned DEFAULT '0' NOT NULL,
sorting int(11) unsigned DEFAULT '0' NOT NULL,
sorting_foreign int(11) unsigned DEFAULT '0' NOT NULL,
KEY uid_local (uid_local),
KEY uid_foreign (uid_foreign)
);
How do you get the selected items in your Extbase controller? Maybe you can use this findByUidListOrderByList function here: http://blog.teamgeist-medien.de/2014/09/typo3-extbase-repository-find-by-multiple-uids-findbyuids.html#comment-90
Let's say I have a model that was soft-deleted and have the following scenario:
// EXISTING soft-deleted Model's properties
$model = [
'id' => 50,
'app_id' => 132435,
'name' => 'Joe Original',
'deleted_at' => '2015-01-01 00:00:00'
];
// Some new properties
$properties = [
'app_id' => 132435,
'name' => 'Joe Updated',
];
Model::updateOrCreate(
['app_id' => $properties['app_id']],
$properties
);
Is Joe Original now Joe Updated?
OR is there a deleted record and a new Joe Updated record?
$variable = YourModel::withTrashed()->updateOrCreate(
['whereAttributes' => $attributes1, 'anotherWhereAttributes' => $attributes2],
[
'createAttributes' => $attributes1,
'createAttributes' => $attributes2,
'createAttributes' => $attributes3,
'deleted_at' => null,
]
);
create a new OR update an exsiting that was soft deleted AND reset the softDelete to NULL
updateOrCreate will look for model with deleted_at equal to NULL so it won't find a soft-deleted model. However, because it won't find it will try to create a new one resulting in duplicates, which is probably not what you need.
BTW, you have an error in your code. Model::updateOrCreate takes array as first argument.
RoleUser::onlyTrashed()->updateOrCreate(
[
'role_id' => $roleId,
'user_id' => $user->id
],
[
'deleted_at' => NULL,
'updated_at' => new \DateTime()
])->restore();
Like this you create a new OR update an exsiting that was soft deleted AND reset the softDelete to NULL
Model::withTrashed()->updateOrCreate([
'foo' => $foo,
'bar' => $bar
], [
'baz' => $baz,
'deleted_at' => NULL
]);
Works as expected (Laravel 5.7) - updates an existing record and "undeletes" it.
I tested the solution by #mathieu-dierckxwith Laravel 5.3 and MySql
If the model to update has no changes (i.e. you are trying to update with the same old values) the updateOrCreate method returns null and the restore() gives a Illegal offset type in isset or empty
I got it working by adding withTrashed so that it will include soft-deleted items when it tries to update or create. Make sure deleted_at is in the fillable array of your model.
$model = UserRole::withTrashed()->updateOrCreate([
'creator_id' => $creator->id,
'user_id' => $user->id,
'role_id' => $role->id,
],[
'deleted_at' => NULL
])->fresh();
try this logic..
foreach ($harga as $key => $value) {
$flight = salesprice::updateOrCreate(
['customerID' => $value['customerID'],'productID' => $value['productID'], 'productCode' => $value['productCode']],
['price' => $value['price']]
);
}
it work for me
I have my custom product attributes in magento.i want to set the product description dynamically.is their any way in magento so that we can find the attribute is custom created by us not by default magento.
i had searched.but not got any success.
Please Help.
Thanks in Advance.
Let's say you have an attribute with code some_code.
Here is how you can check if it's a system attribute or if it's a custom one.
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'some_code');
if ($attribute->getIsUserDefined()) {
//then you created the attribute
}
else {
//then it's a system attribute
}
Use the magento soap/rest api for creating a custom attribute for Product and update with values as well,
$proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
echo "<pre>";
// Create new attribute
$attributeToCreate = array(
"attribute_code" => "new_attribute",
"scope" => "store",
"frontend_input" => "select",
"is_unique" => 0,
"is_required" => 0,
"is_configurable" => 0,
"is_searchable" => 0,
"is_visible_in_advanced_search" => 0,
"used_in_product_listing" => 0,
"additional_fields" => array(
"is_filterable" => 1,
"is_filterable_in_search" => 1,
"position" => 1,
"used_for_sort_by" => 1
),
"frontend_label" => array(
array( "store_id" => 0,"label" => "A new attribute" )
)
);
$attributeId = $proxy->call($sessionId,"product_attribute.create",
array(
$attributeToCreate
)
);
// Update attribute
$attributeToUpdate = array(
"scope" => "global",
"is_unique" => 1,
"is_required" => 1,
"is_configurable" => 1,
"is_searchable" => 1,
"is_visible_in_advanced_search" => 0,
"used_in_product_listing" => 0,
"additional_fields" => array(
"is_filterable" => 01,
"is_filterable_in_search" => 0,
"position" => 2,
"used_for_sort_by" => 0
),
"frontend_label" => array(
array(
"store_id" => 0,
"label" => "A Test Attribute"
)
)
);
$proxy->call(
$sessionId,
"product_attribute.update",
array(
"new_attribute",
$attributeToUpdate
)
);
I hope this will solve your problem..
You can use Magento default API to get default product list
For more details you can refer to following url:
http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalog_product.listOfAdditionalAttributes.html
Code used will be:
$proxy = new SoapClient('localhost/api/soap/?wsdl');
$sessionId = $proxy->login('apiUser', 'apiKey');
$listAttributes = $proxy->call($sessionId, 'product.listOfAdditionalAttributes', array( 'simple', 13 ));
Previously I asked how to ALTER TABLE in Magento setup script without using SQL. There, Ivan gave an excellent answer which I still refer to even now.
However I have yet to discover how to use Varien_Db_Ddl_Table::addColumn() to specify an auto_increment column. I think it has something to do with an option called identity but so far have had no luck.
Is this even possible or is that functionality incomplete?
One can create an autoincrement column like that (at least since Magento 1.6, maybe even earlier):
/** #var $table Varien_Db_Ddl_Table */
$table->addColumn( 'id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
'auto_increment' => true,
'unsigned' => true,
'nullable' => false,
'primary' => true,
), 'ID' );
Instead of "auto_increment", one may also use the keyword "identity".
I think that's something that hasn't been implemented yet.
If you look at the source to addColumn, you can see it looks for a identity/auto_increment option and sets an IDENTITY attribute on the internal column representation.
#File: lib/Varien/Db/Ddl/Table.php
if (!empty($options['identity']) || !empty($options['auto_increment'])) {
$identity = true;
}
$upperName = strtoupper($name);
$this->_columns[$upperName] = array(
'COLUMN_NAME' => $name,
'COLUMN_TYPE' => $type,
'COLUMN_POSITION' => $position,
'DATA_TYPE' => $type,
'DEFAULT' => $default,
'NULLABLE' => $nullable,
'LENGTH' => $length,
'SCALE' => $scale,
'PRECISION' => $precision,
'UNSIGNED' => $unsigned,
'PRIMARY' => $primary,
'PRIMARY_POSITION' => $primaryPosition,
'IDENTITY' => $identity
);
However, if you look at the createTable method on the connection object
#File: lib/Varien/Db/Adapter/Pdo/Mysql.php
public function createTable(Varien_Db_Ddl_Table $table)
{
$sqlFragment = array_merge(
$this->_getColumnsDefinition($table),
$this->_getIndexesDefinition($table),
$this->_getForeignKeysDefinition($table)
);
$tableOptions = $this->_getOptionsDefination($table);
$sql = sprintf("CREATE TABLE %s (\n%s\n) %s",
$this->quoteIdentifier($table->getName()),
implode(",\n", $sqlFragment),
implode(" ", $tableOptions));
return $this->query($sql);
}
you can see _getColumnsDefinition, _getIndexesDefinition, and _getForeignKeysDefinition are used to create a CREATE SQL fragment. None of these methods make any reference to identity or auto_increment, nor do they appear to generate any sql that would create an auto increment.
The only possible candidates in this class are
/**
* Autoincrement for bind value
*
* #var int
*/
protected $_bindIncrement = 0;
which is used to control the increment number for a PDO bound parameter (nothing to do with auto_increment).
There's also a mention of auto_increment here
protected function _getOptionsDefination(Varien_Db_Ddl_Table $table)
{
$definition = array();
$tableProps = array(
'type' => 'ENGINE=%s',
'checksum' => 'CHECKSUM=%d',
'auto_increment' => 'AUTO_INCREMENT=%d',
'avg_row_length' => 'AVG_ROW_LENGTH=%d',
'comment' => 'COMMENT=\'%s\'',
'max_rows' => 'MAX_ROWS=%d',
'min_rows' => 'MIN_ROWS=%d',
'delay_key_write' => 'DELAY_KEY_WRITE=%d',
'row_format' => 'row_format=%s',
'charset' => 'charset=%s',
'collate' => 'COLLATE=%s'
);
foreach ($tableProps as $key => $mask) {
$v = $table->getOption($key);
if (!is_null($v)) {
$definition[] = sprintf($mask, $v);
}
}
return $definition;
}
but this is used to process options set on the table. This auto_increment controls the table AUTO_INCREMENT options, which can be used to control which integer an AUTO_INCREMENT starts at.