Prestashop 1.7.5 module - Update SQL custom table - prestashop-1.7

I'm trying to add a new module to my shop to manage the sql table's values that I made. I can't find a proper guide that show me how to do that because all the forms have values contained in ps_configuration and not within a custom table.
How can I show those values in my form and get to update them?
Thank you if you'll take the time to answer that ^^
So, my form fields are still blank and they don't update my table when I submit.
I added this to "_construct" function:
public function __construct() {
$this->id_sell = $id_sell;
$this->country = $country;
$this->p_cod = $p_cod;
and this to "getContent"
public function getContent() {
$sqlV = 'SELECT * FROM `'._DB_PREFIX_.'mytable` WHERE id_sell = 1';
if ($row = Db::getInstance()->getRow($sqlV))
$country = $row[country];
$p_cod = $row[p_cod];
and last this on "getConfigFormValues":
protected function getConfigFormValues()
{
return array(
'country' => Tools::getValue('country'),
'p_cod' => Tools::getValue('p_cod'),
);
}

So, now I know that a need a class ObjectModel {}, too. Working on it.. and hoping for the best :D

Related

Store a object directly on ORM Laravel?

I search for all the web and don't find a solution other than a custom function that we must make. I develops in Node.js and Laravel backend applications.
And in Node.js we send a object exactly equal to the database columns and we just use Sequelize insert function like this:
async store(model) {
try {
await this.model.city.create(model);
} catch (error) {
throw error;
}
}
We simply use the same object that sending from the app we store like this store(object); and works fine.
In Laravel I always must reference the columns even so the objects have the same attributes from the columns on database, like this:
$categoryObj = new Category;
$categoryObj->name = $category['name'];
$categoryObj->save();
return $categoryObj;
I want to just make something like this:
$categoryObj = new Category;
$categoryObj = $category;
$categoryObj->save();
return $categoryObj;
It is possible?
You can use the create method to insert a new record in the database.
class Category extends Model
{
protected $table = 'table_name';
protected $fillable = ['column_name_1', 'column_name_2', ...];
}
Then for inserting new data:
Example:
$model = YourModel::create([
'field' => 'value',
'another_field' => 'another_value'
]);
In your controller:
$categoryObj = new Category::create($category);
Also, you should look to Laravel's documentation: https://laravel.com/docs/5.8/eloquent#mass-assignment

Laravel nova multiselect edit not working

I'm using laravel nova for creating an admin panel. This is my code.
Laravel nova resource,
use Nova\Multiselect\Multiselect;
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Multiselect::make(__('Day'),'day')
->options($this->getweedkDay()),
];
}
private function getweedkDay(){
$weekDay = [];
$weekDay[1] = __('Sunday');
$weekDay[2] = __('Monday');
$weekDay[3] = __('Tuesday');
$weekDay[4] = __('Wednesday');
$weekDay[5] = __('Thursday');
$weekDay[6] = __('Friday');
$weekDay[7] = __('Saturday');
return $weekDay;
}
I'm using this plugin for multi select and date field is a varchar field in database. The issue is even I can select items and save them in db they will not be able to see in edit view or index view for nova resource. it would be great if someone can help
Use this package instead it's more mature and maintained as well:
https://github.com/optimistdigital/nova-multiselect-field

check if dates are not within range before saving in yii

how can i check if the dates posted in user's form exist in a range before saving? It's suppose to check different scenario date ranges. Below is just one of them. If it doesn't fall into range, it then posts.
is there a better way to do this?
$model = Table::model();
$criteria = new CDbCriteria;
//this is where i don't know how to get the values ($start,$end) from user before posting
$criteria->addCondition('start_date < '.$start);
$criteria->addCondition('end_date > '.$end);
You have to create custom validation function :
In rules add : this rules will work for both insert and update.
public function rules()
{
return array(
array('dateField', 'myCheckdate',),
);
}
In your custom validation function , you can apply your code to check the date range
public function myCheckdate($attribute,$params)
{
if(!$this->hasErrors())
{
if(Condition == false) // place your condition here
{
$this->addError($attribute,'The date is incorrect.');
}
}
}

Order options in admin order view

Using
public function _prepareOptions(Varien_Object $buyRequest, $product, $processMode)
{
$options = parent::_prepareOptions($buyRequest, $product, $processMode);
$options['start_date'] = date here
$options['end_date'] = date here
return $options;
}
in my module I can save some custom options.
I can see them in the "sales_flat_quote_item_option" table.
What i'm stuck on is trying to retrieve and display these values with the order in the admin in this template
"app\design\adminhtml\default\default\template\sales\items\column\name.phtml"
Is there a way to get these options via the $_item variable in the template or will i need to use the orderid and models.
Still not sure which is the correct way but used the buyRequest object
public function prepareForCartAdvanced(Varien_Object $buyRequest, $product = null, $processMode = null)
{
$buyRequest->setData('start_date', $start);
}
Then in the view,
$_item->getBuyRequest()->getData('start_date');

CodeIgniter view problem

This is my first day playing with CI and I really like it so far but have a problem I can't solve on my own. The issue is that I need to generate single view with two controller functions. One div should include selected row by ID from table A and other div should loop foreach on array from table B.
public function index()//div A
{
$data['query'] = $this->db->get_where('beer', array('id' => 1));
$this->load->view('corp/corp_view', $data);
}
public function loadList() //div B
{
$data['q'] = $this->db->get_where('list', array('id' => 1));
$this->load->view('corp/mentor_list_view', $data);
}
I tried to solve this for few hours by creating another view for loadList() and then including it in the the main view like "$this->load->view()" but I'm getting the values from the index() function query table 'beer' not 'list' table as intended. Again I'm new to this and would appreciate your help.
Thank you for your help.
Thanks for the extra info, i can help yah out now.
In Codeigniter if you want to make a function that is not able to be called by the user just precede it with a '_'. So in your case:
public function index()//div A
{
$data['query'] = $this->db->get_where('beer', array('id' => 1));
$data['query2'] = $this->_mySecondQuery();
$this->load->view('corp/corp_view', $data);
}
public function _mySecondQuery() //div B
{
return $this->db->get_where('list', array('id' => 1));
}
Now in the index page you have access to both queries. Btw, I would not suggest doing much DB work in the controller. DB work is meant to be done in Models. For more info on those see: Codeigniter Models

Resources