How to show only per attribute - mysql

I'm trying to make a optics shop with PRESTASHOP, but I'm facing a problem. I've create 4 new columns in product and customer table (left eye diopter, right eye diopter, bridge length, leg length), the same in both tables.
What I want to do is when the shop load the products, compare this variables and if they are the same then show the product. This is to try to filter glasses to the client, only showing him the glasses that are compatible with him.
The original query is the next:
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute,
product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`,
pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image,
il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default,
DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00",
INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p
ON p.`id_product` = cp.`id_product`
'.Shop::addSqlAssociation('product', 'p').
(Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').'
'.Product::sqlStock('p', 0).'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON (product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop
ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.')
LEFT JOIN `'._DB_PREFIX_.'image_lang` il
ON (image_shop.`id_image` = il.`id_image`
AND il.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
AND cp.`id_category` = '.(int)$this->id
.($active ? ' AND product_shop.`active` = 1' : '')
.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');
I'm trying to modify it, but I don't have very clear how to. And in consecuence, I'm doing wrong things. I've added the next left joins to the query.
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.`id_product` = p.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` attr ON attr.`id_attribute` = pac.`id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` attr_lang ON (attr_lang.`id_attribute` = pac.`id_attribute` AND attr_lang.`id_lang` = '.(int)$id_lang.')LEFT JOIN `'._DB_PREFIX_.'attribute_group` attr_group ON attr_group.`id_attribute_group` = attr.`id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` attr_group_lang ON attr_group_lang.`id_attribute_group` = attr.`id_attribute_group`
Thank you for any advice.
EDIT:
The new fields of the products are features inside prestashop (there aren't inside product table) sorry for the mistake.
I put the data model of prestashop for your info.
EDIT 2:
I'm trying now to achieve this by using a module, so my php file of my module has the next code. I've follow the code from CategoryController.php, but I don't know how to remove a product if the conditions are not satisfied.
<?php
if (!defined('_PS_VERSION_'))
exit;
class glassOptics extends Module
{
/* #var boolean error */
protected $_errors = false;
public function __construct()
{
$this->name = 'glassOptics';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'MAOL';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('glassOptics');
$this->description = $this->l('...');
}
public function install()
{
if (!parent::install() OR
!$this->veopticasCustomerDB('add') OR
!$this->veopticasProductDB('add') OR
!$this->registerHook('hookActionProductListOverride')
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() OR !$this->veopticasCustomerDB('remove') OR !$this->veopticasProductDB('remove'))
return false;
return true;
}
public function veopticasCustomerDB($method)
{
switch ($method) {
case 'add':
$sql = 'CREATE TABLE IF EXISTS `'._DB_PREFIX_.'customer_optics_data` (
`id_customer` int(10) UNSIGNED NOT NULL,
`left_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`right_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`bridge` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_width` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_height` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
break;
case 'remove':
$sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_ . 'customer_optics_data`';
break;
}
if(!Db::getInstance()->Execute($sql))
return false;
return true;
}
public function veopticasProductDB($method)
{
switch ($method) {
case 'add':
$sql = 'CREATE TABLE IF EXISTS `'._DB_PREFIX_.'product_optics_data` (
`id_product` int(10) UNSIGNED NOT NULL,
`left_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`right_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`bridge` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_width` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_height` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
break;
case 'remove':
$sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_ . 'product_optics_data`';
break;
}
if(!Db::getInstance()->Execute($sql))
return false;
return true;
}
public function hookActionProductListOverride($params)
{
$customer_settings = glassOptics::getCustomerSettings($this->context->customer);
if ($customer_settings) {
// Inform the hook was executed
$params['hookExecuted'] = true;
// Filter products here, you are now overriding the default
// functionality of CategoryController class.
// You can see blocklayered module for more details.
if ((isset($this->context->controller->display_column_left) && !$this->context->controller->display_column_left)
&& (isset($this->context->controller->display_column_right) && !$this->context->controller->display_column_right))
return false;
global $smarty;
if (!Configuration::getGlobalValue('PS_LAYERED_INDEXED'))
return;
$categories_count = Db::getInstance()->getValue('
SELECT COUNT(*)
FROM '._DB_PREFIX_.'layered_category
WHERE id_category = '.(int)Tools::getValue('id_category', Tools::getValue('id_category_layered', Configuration::get('PS_HOME_CATEGORY'))).'
AND id_shop = '.(int) Context::getContext()->shop->id
);
if ($categories_count == 0)
return;
// List of product to overrride categoryController
$params['catProducts'] = array();
$selected_filters = $this->getSelectedFilters();
$filter_block = $this->getFilterBlock($selected_filters);
$title = '';
if (is_array($filter_block['title_values']))
foreach ($filter_block['title_values'] as $key => $val)
$title .= ' > '.$key.' '.implode('/', $val);
$smarty->assign('categoryNameComplement', $title);
$this->getProducts($selected_filters, $params['catProducts'], $params['nbProducts'], $p, $n, $pages_nb, $start, $stop, $range);
// Need a nofollow on the pagination links?
$smarty->assign('no_follow', $filter_block['no_follow']);
foreach ($params['nbProducts'] as $product) {
$product_settings = glassOptics::getProductSettings($product);
if($product_settings){
$same_bridge = ($product_settings->bridge == $customer_settings->bridge ? true : false);
$same_leg = ($product_settings->leg == $customer_settings->leg ? true : false);
$same_glass_width = ($product_settings->glass_width == $customer_settings->glass_width ? true : false);
$same_glass_heigth = ($product_settings->glass_heigth == $customer_settings->glass_heigth ? true : false);
}
}
}
}
}

I'd recommend taking a different approach and use a module for that. You would create a module that upon installation creates a table_ something like customer_optics_data. Table structure could look something like this:
CREATE TABLE `'._DB_PREFIX_.'customer_optics_data` (
`id_customer` int(10) UNSIGNED NOT NULL,
`left_eye_diopter` int(10) UNSIGNED NOT NULL,
`right_eye_diopter` int(10) UNSIGNED NOT NULL,
`bridge_length` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg_length` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;
Then your module would hook onto actionProductListOverride hook, and this is where you would perform the check:
public function hookActionProductListOverride($params)
{
$customer_settings = MyDiopterModuleHelperClass::getCustomerSettings($this->context->customer);
if ($customer_settings) {
$params['hookExecuted'] = true;
// Filter products here, you are now overriding the default
// functionality of CategoryController class.
// You can see blocklayered module for more details.
}
}
The module would have a helper class MyDiopterModuleHelperClass that is there to register and obtain data to/from the customer_optics_data table.
This way you're not overriding the core, your updates will still function as normal, the worst thing that can happen is if the hook is suddenly removed from the future versions of PrestaShop, which is unlikely.
The module would also employ the following hooks:
displayCustomerIdentityForm - to display additional fields in My Personal information. This is where your customers would input their information for the module.
actionObjectCustomerAddAfter - this is where you would fetch that data from $_POST and save it in module's table
actionObjectCustomerUpdateAfter - this is where you would update the data if it has been changed by the customer or insert the data if for some reason it's not there.
Optionally, you could also hook the module onto
displayAdminCustomersForm - to display the additional fields in the customers form in your back office.

Related

Auto increment is lower than total rows

I created a php script that takes values from 1 table and inserts them into another.
This executes pretty quickly (1000 rows per second).
Everything went well. There are no duplicates in the table and I think all the values from the other table are inserted.
But something strange caught my attention:
There are a total of 903388 rows in the table but the highest ID that has auto-increment enabled is 898582.
That's a difference of 4806.
The other table has almost the same amount of items as the ID but that is always off by a bit due to the original table always getting new values.
Due to such a large database a MRE could not be produced
So now my question is:
How can the auto-increment value be lower than the total amount of rows?
The datatype of the original tables ID:
bigint(20)
The datatype of the IMAGES table ID:
int(11)
Show create table IMAGES;
CREATE TABLE `IMAGES` (
`ID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`SRC` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL COMMENT 'Pad naar afbeelding',
`VERWIJDERD` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0=image is online, 1=image is verwijderd',
`DATUM` datetime NOT NULL DEFAULT current_timestamp() COMMENT 'Datum van upload',
`IP` varchar(15) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL COMMENT 'ip van uploader',
`SITE` tinyint(1) DEFAULT NULL COMMENT 'site waar image is geupload',
`OID` int(11) DEFAULT NULL COMMENT 'occasion id',
`POSITIE` int(11) NOT NULL DEFAULT 0 COMMENT 'sorteer id',
`TYPE` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0=normaal 1=schade',
PRIMARY KEY (`ID`),
KEY `OID` (`OID`)
) ENGINE=InnoDB AUTO_INCREMENT=898583 DEFAULT CHARSET=latin1
My PHP script that fills the table:
<form action="" method="post">
<label for="fname">Rijen:</label><br/>
offset<input type="number" name="offset"><br/>
amount<input type="number" name="amount"><br/>
<button type="submit" name="submit">Go</button>
</form><br/>
<?php
$per_insert = 100;
if(isset($_POST['submit'])){
echo "Offset: ".$_POST['offset']."<br/>";
echo "Limit: ".$_POST['amount']."<br/>";
$msc = microtime(true);
$count = (is_numeric($_POST['offset']) ? (int)$_POST['offset'] : 0);
$amount = (is_numeric($_POST['amount']) ? (int)$_POST['amount'] : 0);
$qcount = 0;
$filter_array = array('images/occ/', null, '');
for ($i = $count+$per_insert; $i <= $amount; $i+=$per_insert){
$valuesq = array();
$olddataq = $mysqli->query("SELECT `ID`,SITE,DATUM,IP,`IMG_1`,`IMG_2`,`IMG_3`,`IMG_4`,`IMG_5`,`IMG_6`,`IMG_7`,`IMG_8`,`IMG_9`,`IMG_10`,`IMG_11`,`IMG_12`,`IMG_13`,`IMG_14`,`IMG_15`,`IMG_16`,`IMG_17`,`IMG_18`,`IMG_19`,`IMG_20`,`IMGS_1`,`IMGS_2`,`IMGS_3`,`IMGS_4`,`IMGS_5`,`IMGS_6`,`IMGS_7`,`IMGS_8`,`IMGS_9`,`IMGS_10`,`IMGS_11`,`IMGS_12`,`IMGS_13`,`IMGS_14`,`IMGS_15`,`IMGS_16`,`IMGS_17`,`IMGS_18`,`IMGS_19`,`IMGS_20` FROM `OCCASIONS` LIMIT ".$per_insert." OFFSET ".$count.";");
$qcount++;
$schade = $normaal = 0;
while($olddata = $olddataq->fetch_assoc()){
$olddata = array_diff($olddata, $filter_array);
$id = $olddata['ID'];
$datum = $olddata['DATUM'];
$ip = $olddata['IP'];
$site = $olddata['SITE'];
unset($olddata['DATUM']);
unset($olddata['ID']);
unset($olddata['IP']);
unset($olddata['SITE']);
while ($data = current($olddata)) {
$key = explode('_',key($olddata));
if($key[0] == 'IMG'){
//normale image
$datacheck = check_fotodata($data, $id, $key[1], 0);
if($datacheck === false){
$valuesq[] = "('".$data."','".$datum."','".$ip."',".$site.",".$id.", ".$key[1].", 0,0)";
}else{
$valuesq[] = $datacheck;
}
}else{
//schade image
$datacheck = check_fotodata($data, $id, $key[1], 1);
if($datacheck === false){
$valuesq[] = "('".$data."','".$datum."','".$ip."',".$site.",".$id.", ".$key[1].", 1,0)";
}else{
$valuesq[] = $datacheck;
}
}
next($olddata);
}
}
$count += $per_insert;
//var_dump($valuesq);
$mysqli->query("INSERT INTO IMAGES (SRC, DATUM, IP, SITE, OID, POSITIE, TYPE, VERWIJDERD) VALUES ". implode(",", $valuesq));
$qcount++;
}
$msc = microtime(true)-$msc;
echo "buildtime: <br/>";
echo $msc . ' s<br/>'; // in seconds
echo ($msc * 1000) . ' ms<br/>'; // in millseconds
echo $qcount . "<br/>";
$msc = microtime(true);
}
function check_fotodata($image, $oid, $pos, $type){
global $qcount, $mysqli;
$checkdataq = $mysqli->query("SELECT * FROM FOTODATA WHERE KID = ". $oid ." AND IMG = '". $image ."'");
$qcount++;
if($checkdataq->num_rows > 0){
$checkdata = $checkdataq->fetch_assoc();
if($checkdata['INFO'] == 'Verwijderd'){
$del = 1;
}else{
$del = 0;
}
return "('".$checkdata['IMG']."', '".$checkdata['DATUM']."', '".$checkdata['IP']."', '".$checkdata['SITE']."', '".$checkdata['KID']."',".$pos.",".$type.",".$del.")";
}else{
return false;
}
}
Please check count of table by below query first then compare. Count of table and max id should be equal if you use auto_increment_offset 1 and auto_increment_increment 1 in MySQL.
select count(*) from IMAGES;
Please comment what you find.

Laravel - How do update a table immediately records are saved in it

In my Laravel-5.8, I have this table.
CREATE TABLE `appraisal_goal_types` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
`max_score` int(11) DEFAULT 0,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Then I created this controller to store record in another table.
public function store(StoreAppraisalGoalRequest $request)
{
$appraisalStartDate = Carbon::parse($request->appraisal_start_date);
$appraisalEndDate = Carbon::parse($request->appraisal_end_date);
$userCompany = Auth::user()->company_id;
$employeeId = Auth::user()->employee_id;
$identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
try {
$goal = new AppraisalGoal();
$goal->goal_type_id = $request->goal_type_id;
$goal->appraisal_identity_id = $request->appraisal_identity_id;
$goal->employee_id = $employeeId; //$request->employees_id
$goal->weighted_score = $request->weighted_score;
$goal->goal_title = $request->goal_title;
$goal->goal_description = $request->goal_description;
$goal->company_id = Auth::user()->company_id;
$goal->created_by = Auth::user()->id;
$goal->created_at = date("Y-m-d H:i:s");
$goal->is_active = 1;
if ($request->appraisal_doc != "") {
$appraisal_doc = $request->file('appraisal_doc');
$new_name = rand() . '.' . $appraisal_doc->getClientOriginalExtension();
$appraisal_doc->move(public_path('storage/documents/appraisal_goal'), $new_name);
$goal->appraisal_doc = $new_name;
}
$goal->save();
$parentids = DB::table('appraisal_goal_types')->select('parent_id')->whereNotNull('parent_id')->where('company_id', $userCompany)->where('id', $goal->goal_type_id)->first();
$parentid = $parentids->id;
$goal->update(['parent_id' => $parentid]);
}
As soon as the record is saved, I want to quickly query appraisal_goal_types
$parentids = DB::table('appraisal_goal_types')->select('parent_id')->whereNotNull('parent_id')->where('id', $goal->goal_type_id)->first();
$parentid = $parentids->id;
$goal->update(['parent_id' => $parentid]);
and update the record.
I need only one row there where the answer is true. I used the code above, but nothing is happening.
How do I resolve this?
Thank you
Try like this,
$parentids = DB::table('appraisal_goal_types')->select('parent_id')->whereNotNull('parent_id')->where('company_id', $userCompany)->where('id', $goal->goal_type_id)->first();
$parentid = $parentids->id;
$goal->parent_id = $parentid;
$goal->save();
There is an another solution like this,
$parentids = DB::table('appraisal_goal_types')->select('parent_id')->whereNotNull('parent_id')->where('company_id', $userCompany)->where('id', $goal->goal_type_id)->first();
$parentid = $parentids->id;
AppraisalGoal::where('id', $goal->id)->update(['parent_id' => $parentid]);
Both will works. And let me know if you solved the issue

Storing 2 same id of post table with different language into database

I'm building a multi language website. In this case just 2 languages, it's Indonesian and English using Laravel.
I have posts table, it will store id of each post and post_translations table is to store local, title, and description of post.
I got the problem when storing data into database. I don't have any idea how to store post without inc the id except I've added 2 same post with Indonesian and English.
This is the result (wrong)
posts table
id
1
2
post_translations table
id post_id locale title
1 1 en fisrt post
2 2 id post yang pertama
Expexted result
posts table
id
1
post_translations table
id post_id locale title
1 1 en fisrt post
2 1 id post yang pertama
PostController
public function store(Request $request) {
$this->validate($request, [
'title' => 'required',
'slug' => 'required',
'content' => 'required'
]);
$post = new Post;
$post->title = $request->title;
$post->slug = $request->slug;
$post->content = $request->content;
$post->save();
return redirect()->route('post.index');
}
Ok, so here we go (please note that this is not the only way):
install spatie/laravel-translatable with
composer require spatie/laravel-translatable
**Note: for native spatie/laravel-translatable go to version 2 **
create a table with this structure:
CREATE TABLE articles (
id int(10) UNSIGNED NOT NULL,
title text COLLATE utf8_unicode_ci,
slug text COLLATE utf8_unicode_ci,
content text COLLATE utf8_unicode_ci,
created_at timestamp NULL DEFAULT NULL,
updated_at timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Note: beter use a migration. I just exported a table I did earlier to test
Insert the data in the datatabase in json format like this:
INSERT INTO `pages` (`id`, `title`, `slug`, `content`, `created_at`, `updated_at`) VALUES
(1, '{"ro":"Acas\\u0103","en":"Home"}', NULL, '{"ro":"<p><strong>Test title<\\/strong><\\/p>\\r\\n\\r\\n<p>Test content romanian<\\/p>\\r\\n","en":"<p><strong>test title english<\\/strong><\\/p>\\r\\n\\r\\n<p>test content english.<\\/p>\\r\\n"}', '2017-04-03 11:45:56', '2017-04-03 12:15:16');
Now create the blade to edit, update, create show etc. To get the language do something like this in the blade:
{{ $data->title }}
{!! nl2br($data->content) !!}
And in the controller:
add something like this:
/**
* Generate the field by language
*
* #param Model $entry the item selected from the database
*
* #return array
*/
public function getTranslatableFields($fields)
{
$inputs = [];
$languages = $this->getLanguages();
foreach ($languages as $language) {
foreach ($fields as $field) {
$inputs[] = [
'name' => "{$field['name']}[{$language->abbr}]",
'label' => $field['label'] . " ($language->abbr)",
'lang' => $language->abbr,
'type' => array_key_exists('type', $field) ? $field['type'] : 'text'
];
}
}
return $inputs;
}
I added this function in a LangTrait. Since I also use backpack for laravel I did some more things.
For edit I added this method in the trait:
/**
* Show the form for editing the specified resource.
*
* #param int $id the item's identifier
*
* #return Response
*/
public function edit($id)
{
$data['entry'] = Model::find($id);
$data['title'] = trans('lang_file.edit').' '.$this->entity_name; // name of the page
$data['fields'] = $this->getMultiLangFields($data['entry']);
$data['id'] = $id;
return view('crud::edit', $data);
}
/**
* Generate the field by language
*
* #param Model $entry the item selected from the database
*
* #return array
*/
protected function getMultiLangFields($entry)
{
$fields['id'] = ['name' => 'id', 'type' => 'hidden', 'value' => $entry->id];
foreach ($this->crud->update_fields as $key => $field) {
$value = null;
if (array_key_exists('lang', $field)) {
$name = preg_replace('/(\[\w{2}\])$/i', '', $field['name']);
$value = $entry->getTranslation($name, $field['lang']);
}
$fields[$key] = array_merge($field, ['value' => $value]);
}
return $fields;
}
/**
* Get the application active languages
*
* #return \Backpack\LangFileManager\app\Models\Language
*/
protected function getLanguages()
{
return Language::whereActive(1)->orderBy('default', 'desc')->get();
}
Finally in my main controller I did:
use LangTrait; (contains everything above)
In construct I added this:
$this->getTranslatableFields($fields)
where $fields it's the list of fields I need
All methods should be adapted to you html format. As I said I use backpack for laravel and fields are formatted accordingly
And finally for the getLanguage file to work I created a new table and a model in the DB:
Model:
class Language extends Model
{
protected $table = 'languages';
protected $fillable = ['name', 'flag', 'abbr', 'native', 'active', 'default'];
public $timestamps = false;
public static function getActiveLanguagesArray()
{
$active_languages = self::where('active', 1)->get()->toArray();
$localizable_languages_array = [];
if (count($active_languages)) {
foreach ($active_languages as $key => $lang) {
$localizable_languages_array[$lang['abbr']] = $lang;
}
return $localizable_languages_array;
}
return config('laravellocalization.supportedLocales');
}
public static function findByAbbr($abbr = false)
{
return self::where('abbr', $abbr)->first();
}
}
Table:
CREATE TABLE `languages` (
`id` int(10) UNSIGNED NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`app_name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`flag` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`abbr` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
`script` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`native` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`active` tinyint(3) UNSIGNED NOT NULL DEFAULT '1',
`default` tinyint(3) UNSIGNED NOT NULL DEFAULT '0',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Data in the table:
INSERT INTO `languages` (`id`, `name`, `app_name`, `flag`, `abbr`, `script`, `native`, `active`, `default`, `created_at`, `updated_at`, `deleted_at`) VALUES
(1, 'English', 'english', '', 'en', 'Latn', 'English', 1, 0, NULL, NULL, NULL),
(2, 'Romanian', 'romanian', '', 'ro', 'Latn', 'română', 1, 1, NULL, NULL, NULL);
Since I did this through a package I kind of messed around a little bit with the code.
Now, for the spatie/laravel-translatable package version:
set up the service provider in config/app.php add this in the providers array:
Spatie\Translatable\TranslatableServiceProvider::class,
In the model Articles add use HasTranslations; like this:
use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;
class NewsItem extends Model
{
use HasTranslations;
public $translatable = ['name']; // list the columns you want to be translatable (will have json format)
}
save a new entry and use it:
$article = new Article;
$article->setTranslation('name', 'en', 'Updated name in English')
->setTranslation('name', 'nl', 'Naam in het Nederlands');
$article->save();
$article->name; // Returns 'Name in English' given that the current app locale is 'en'
$article->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'
app()->setLocale('nl');
$article->name; // Returns 'Naam in het Nederlands'
examples from: https://github.com/spatie/laravel-translatable
Database table format is as stated above in the first version
If it does not work out let me know and I'll look at your code.
I think you should change your tables strucuture:
posts : id, slug.
post_translations : id, post_id, locale, title, content
Also add relation to your Post model:
public function translations()
{
return $this->hasMany(PostTranslation::class, 'post_id');
}
And update your controller:
$post = new Post;
$post->slug = $request->slug;
$post->save();
$post->translations()->create([
'locale' => 'en', //or grab it from $request
'title' => $request->title,
'content' => $request->content
])
It will create your post and add translation to it

LEFT JOIN in ZF2 using TableGateway

I have a table:
*CREATE TABLE IF NOT EXISTS `blogs_settings` (
`blog_id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`meta_description` text NOT NULL,
`meta_keywords` text NOT NULL,
`theme` varchar(25) NOT NULL DEFAULT 'default',
`is_active` tinyint(1) NOT NULL DEFAULT '1',
`date_created` int(11) NOT NULL,
PRIMARY KEY (`blog_id`),
KEY `owner_id` (`owner_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;*
And the second table:
*CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(128) NOT NULL,
`sex` tinyint(1) NOT NULL,
`birthday` date NOT NULL,
`avatar_id` int(11) DEFAULT NULL,
`user_level` tinyint(1) NOT NULL DEFAULT '1',
`date_registered` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL DEFAULT '0',
`is_banned` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`),
KEY `is_active` (`is_active`),
KEY `user_level` (`user_level`),
KEY `is_banned` (`is_banned`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;*
How may I select all the fields from blogs_settings table and join only the 'username' field from the users table using TableGateway in ZF2, on blogs_settings.owner_id = users.user_id. Thanks in advance. Your help is much appreciated.
EDIT:
namespace Object\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
class BlogsSettingsTable {
protected $tableGateway;
protected $select;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
$this->select = new Select();
}
public function getBlogs($field = '', $value = '') {
$resultSet = $this->tableGateway->select(function(Select $select) {
$select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
});
return $resultSet;
}
public function getBlog($blogID) {
$id = (int) $blogID;
$rowset = $this->tableGateway->select(array('blog_id' => $id));
$row = $rowset->current();
if (!$row) {
throw new Exception('Could not find row with ID = ' . $id);
}
return $row;
}
public function addBlog(BlogsSettings $blog) {
$data = array(
'owner_id' => $blog->owner_id,
'title' => $blog->title,
'meta_description' => $blog->meta_description,
'meta_keywords' => $blog->meta_keywords,
'theme' => $blog->theme,
'is_active' => $blog->is_active,
'date_created' => $blog->date_created,
);
$this->tableGateway->insert($data);
}
public function deleteBlog($blogID) {
return $this->tableGateway->delete(array('blog_id' => $blogID));
}
}
With this, it executes the following query:
SELECT blogs_settings.*, users.username AS username FROM blogs_settings INNER JOIN users ON blogs_settings.owner_id = users.user_id
but the resultSet does not contain the username field from the joined 'users' table. However, when I run the query in phpmyadmin, everything is okay and I have the 'username' field from the 'users' table joined. What's the problem?
EDIT 2
ok, I now tried the following:
public function getBlogs() {
$select = $this->tableGateway->getSql()->select();
$select->columns(array('blog_id', 'interest_id', 'owner_id', 'title', 'date_created'));
$select->join('users', 'users.user_id = blogs_settings.owner_id', array('username'), 'left');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
the executed query is:
SELECT `blogs_settings`.`blog_id` AS `blog_id`, `blogs_settings`.`interest_id` AS `interest_id`, `blogs_settings`.`owner_id` AS `owner_id`, `blogs_settings`.`title` AS `title`, `blogs_settings`.`date_created` AS `date_created`, `users`.`username` AS `username` FROM `blogs_settings` LEFT JOIN `users` ON `users`.`user_id` = `blogs_settings`.`owner_id`
When I run it into phpmyadmin, it joins the username field from the users table. When in zf2, it doesn't.
Here's the dump of the whole object:
Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => Object\Model\BlogsSettings Object
(
[blog_id] =>
[interest_id] =>
[owner_id] =>
[title] =>
[meta_description] =>
[meta_keywords] =>
[theme] =>
[is_active] =>
[date_created] =>
)
[returnType:protected] => arrayobject
[buffer:protected] =>
[count:protected] => 1
[dataSource:protected] => Zend\Db\Adapter\Driver\Pdo\Result Object
(
[statementMode:protected] => forward
[resource:protected] => PDOStatement Object
(
[queryString] => SELECT `blogs_settings`.`blog_id` AS `blog_id`, `blogs_settings`.`interest_id` AS `interest_id`, `blogs_settings`.`owner_id` AS `owner_id`, `blogs_settings`.`title` AS `title`, `blogs_settings`.`date_created` AS `date_created`, `users`.`username` AS `username` FROM `blogs_settings` LEFT JOIN `users` ON `users`.`user_id` = `blogs_settings`.`owner_id`
)
[options:protected] =>
[currentComplete:protected] =>
[currentData:protected] =>
[position:protected] => -1
[generatedValue:protected] => 0
[rowCount:protected] => 1
)
[fieldCount:protected] => 6
[position:protected] =>
)
Up... any ideas?
Adding to #samsonasik's answer and addressing the issues in its comments. You won't be able to get the joined values out of what is returned from that statement. That statement returns the model object which won't have the joined rows. You'll need to execute it as SQL at a level which will prepare it as raw SQL and return you each resulting row as an array rather than an object:
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->columns(array('column_name_yourtable'));
$sqlSelect->join('othertable', 'othertable.id = yourtable.id', array('column_name_othertable'), 'left');
$statement = $this->tableGateway->getSql()->prepareStatementForSqlObject($sqlSelect);
$resultSet = $statement->execute();
return $resultSet;
//then in your controller or view:
foreach($resultSet as $row){
print_r($row['column_name_yourtable']);
print_r($row['column_name_othertable']);
}
if you're using TableGateway, you can select join like this
$sqlSelect = $this->tableGateway->getSql()->select();
$sqlSelect->columns(array('column_name'));
$sqlSelect->join('othertable', 'othertable.id = yourtable.id', array(), 'left');
$resultSet = $this->tableGateway->selectWith($sqlSelect);
return $resultSet;
You have to include username field in the BlogsSetting Model that is used as model from BlogsSettingTable (The TableGateway)
class BlogsSetting {
public $blog_id;
public $interest_id;
public $owner_id;
public $title;
public $meta_description;
public $meta_keywords;
public $theme;
public $is_active;
public $date_created;
public $username;
public function exchangeArray($data)
{
// Create exchangeArray
}
}
Hope this helps
This is the exact need for both Join and Where clauses with tableGateway.
public function getEmployeefunctionDetails($empFunctionId) {
$empFunctionId = ( int ) $empFunctionId;
//echo '<pre>'; print_r($this->tableGateway->getTable()); exit;
$where = new Where();
$where->equalTo('FUNCTION_ID', $empFunctionId);
$sqlSelect = $this->tableGateway->getSql()->select()->where($where);
$sqlSelect->columns(array('FUNCTION_ID'));
$sqlSelect->join('DEPARTMENTS', 'DEPARTMENTS.DEPARTMENT_ID = EMPLOYEE_FUNCTIONS.DEPARTMENT_ID', array('DEPARTMENT_ID','DEPARTMENT_NAME'), 'inner');
$sqlSelect->join('ROLES', 'ROLES.ROLE_ID = EMPLOYEE_FUNCTIONS.ROLE_ID', array('ROLE_ID','ROLE_NAME'), 'inner');
//echo $sqlSelect->getSqlString(); exit;
$resultSet = $this->tableGateway->selectWith($sqlSelect);
if (! $resultSet) {
throw new \Exception ( "Could not find row $empFunctionId" );
}
return $resultSet->toArray();
}
In your class inherited from AbstractTableGateway u can use Select with Closure like this:
use Zend\Db\Sql\Select;
...
public function getAllBlockSettings()
{
$resultSet = $this->select(function(Select $select) {
$select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
});
return $resultSet;
}
Give it a try:
namespace Object\Model;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Sql\Select;
class BlogsSettingsTbl extends AbstractTableGateway {
public function __construct($adapter) {
$this->table = 'blogs_settings';
$this->adapter = $adapter;
$this->initialize();
}
public function fetchAll() {
$where = array(); // If have any criteria
$result = $this->select(function (Select $select) use ($where) {
$select->join('users', 'blogs_settings.owner_id = users.user_id', array('username'));
//echo $select->getSqlString(); // see the sql query
});
return $result;
}
}
Add to 'getServiceConfig()' in Module.php:
'Object\Model\BlogsSettingsTbl' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new BlogsSettingsTbl($dbAdapter); // <-- also add this to 'USE' at top
return $table;
},
since the OP hasn't accepted any answer, I'll try to give the solution.
I face the same solution as the OP states and the only way to fix it is by adding this line to the model class (in this case this might be 'Blogsetttings.php').
$this->username= (!empty($data['username'])) ? $data['username'] : null;
you should add above line to the exchangeArray() method.
Hope it helps

mysql_fetch_array() problem

So I have 3 DB tables that are all identical in every way (data is different) except the name of the table. I did this so I could use one piece of code with a switch like so:
function disp_bestof($atts) {
extract(shortcode_atts(array(
'topic' => ''
), $atts));
$connect = mysql_connect("localhost","foo","bar");
if (!$connect) { die('Could not connect: ' . mysql_error()); }
switch ($topic) {
case "attorneys":
$bestof_query = "SELECT * FROM attorneys p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_TopAttorneys');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
case "physicians":
$bestof_query = "SELECT * FROM physicians p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_TopDocs');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
case "dining":
$bestof_query = "SELECT * FROM restaurants p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_DiningAwards');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
default:
$bestof_query = "switch on $best did not match required case(s)";
break;
}
$category = '';
while( $result = mysql_fetch_array($query) ) {
if( $result['category'] != $category ) {
$category = $result['category'];
//echo "<div class\"category\">";
$bestof_content .= "<h2>".$category."</h2>\n";
//echo "<ul>";
Now, this whole thing works PERFECT for the first two cases, but the third one "dining" breaks with this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource ... on line 78
Line 78 is the while() at the bottom. I have checked and double checked and can't figure what the problem is. Here's the DB structure for 'restaurants':
CREATE TABLE `restaurants` (
`id` int(10) NOT NULL auto_increment,
`restaurant` varchar(255) default NULL,
`address1` varchar(255) default NULL,
`address2` varchar(255) default NULL,
`city` varchar(255) default NULL,
`state` varchar(255) default NULL,
`zip` double default NULL,
`phone` double default NULL,
`URI` varchar(255) default NULL,
`neighborhood` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=249 DEFAULT CHARSET=utf8
Does anyone see what I'm doing wrong here? I'm passing "dining" to the function and as I said before, the first two cases in the switch work fine.
I'm sure it's something stupid...
You should always initialize the variable you use to some (null) value and then check for it before using it. My guess is that your third case (dining) never gets executed because of some misspelled identifier or something. This causes default: to run, after which your while() will execute anyway. However, $query is not set to anything useful.
Therefore, you should throw an exception or otherwise break execution in the default: handler. Or, you may initialize $query = null; before the switch() and only do the while() loop when $query !== null.
On a related note: you might code more efficient when you instead use the following (note the exception handler):
$db_name = null;
$table = null;
switch ($topic) {
case "attorneys":
$db_name = 'roanoke_BestOf_TopAttorneys';
$table = 'attorneys'
break;
case "physicians":
$db_name = 'roanoke_BestOf_TopDocs';
$table = 'physicians'
break;
case "dining":
$db_name = 'roanoke_BestOf_DiningAwards';
$table = 'restaurants'
break;
default:
throw new Exception("Unknown topic.");
break;
}
$bestof_query = "SELECT * FROM $table p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db($db_name);
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
You're getting a sql error on that query. You should echo your mysql error and review it to fix your query. The warning you're getting is because you're passing a boolean false to mysql_fetch_assoc() which is expecting a result set. mysql_query() returns false if there is an error.
Look at your query code - you run $bestof_query regardless of whether it has been set to valid SQL. My first guess is that you're misspelling 'dining' somewhere and getting the default case.
Also, double check that your database names are correct (they are fairly complicated) and that all databases have the same permissions. Are you checking whether $db is true?