updating timestamp field in mysql using joomla component - joomla

I'm working on joomla component (com_book) version 3.0. In that I'm trying to insert books using form to database, in database I'm having updated_at column for that column I'm using timestamp datatype. Here updated_at column working fine when i'm inserting and updating books using database and inserting books using forms then it's working fine but when I'm trying updating books using forms that time update_at column not updating.
Can anyone explain what mistake I made in that process?

Another method is creating a custom field for that,
use this field code in your book.xml form file
<fieldset
addfieldpath="/administrator/components/com_book/models/fields"
>
..
..
<field name="timestamp" type="lastmodified"
label="" description="" />
..
..
</fieldset>
and create a file called, lastmodified.php
in your /administrator/components/com_book/models/fields folder
<?php
defined('JPATH_BASE') or die;
jimport('joomla.form.formfield');
/**
* Supports an HTML form field
*/
class JFormFieldLastModified extends JFormField
{
/**
* The form field type.
*
* #var string
* #since 1.6
*/
protected $type = 'lastmodified';
/**
* Method to get the field input lastmodified.
*
*/
protected function getInput()
{
// Initialize variables.
$html = array();
$old_time_updated = $this->value;
if ($old_time_updated) {
$jdate = new JDate($old_time_updated);
$pretty_date = $jdate->format(JText::_('DATE_FORMAT_LC2'));
}
$time_updated = date("Y-m-d H:i:s");
$html = '<input type="hidden" name="'.$this->name.'" value="'.$time_updated.'" />';
return $html;
}
}
?>

Paste this function in the Table file, of your table for example
Joomla-Root/Administrator/com_book/table/book.php
if there is already a store function, then edit that and add these lines alone man, that will do the job,
public function store($updateNulls = false)
{
$date = JFactory::getDate();
// this is your primary key maybe id or book_id
if ($this->book_id) {
// Assigning the last modified date to your timestamp field
$this->timestamp = $date->toSql();
}
// Attempt to store the user data. - just leave the rest to parent function
return parent::store($updateNulls);
}

In save function after data binding you can set the updated_at
// Bind the data to the table
if (!$table->bind($post))
{
//display error
}
//after binding write this line-
$table->updated_at = date('Y-m-d H:i:s');
let me know if this not work.

The Joomla Component Creator can build Joomla 3.0 components and save you messing with simple stuff like this.
Take a look: http://www.notwebdesign.com/joomla-component-creator/
It's free for one table. If nothing else you can copy paste code from that or peak and see how to develop yourself.

Related

how to pick single data row from DB using link (A tag)

here is my Data Requesting link
<input type="Hidden" name="addid" value="{{$add->id}}">
{{ __('View More') }}
then this is connect with Route like this
Route::get('/PickAdd/{id}', 'MAddController#view')->name('PickAdd');
controller is like this
public function view($id)
{ //dd($id);
$Padd = Madd::find('$id');
return view('adds.add',compact('Padd','id'));
}
this controlling function not working.
but I change like this
$Padd = Madd::find('22');
then it works,
that mean $id =22
if I run DD, result is null
according to my knowledge my code cant catch passing data from front end, can you help me to solve this
You are passing $id as a string from your controller
Change
$Padd = Madd::find('$id');
to
$Padd = Madd::find($id);
Don't use find() method, in case the record does not exist. Use first()
$Padd = Madd::where('id',$id)->first();
That works when the record exists or not.
Also, look in the Madd model is you defined another primary key other than id, like
protected $primaryKey = 'some-other-key';
because that would force Eloquent to look for that instead of the id

Magento 2.3 graphql get custom attribute option values

I’m pretty frustrated with the graphql api of Magento. How is it not possible to receive the values of a custom attribute. I mean it’s easy to make a custom select-option attribute and making it available through graphql but you are only getting the integer value.
What’s the best procedure to following
1. Query the complete attribute to get the value
2. Extending the graphql schema (this involves a developer every time the client changes something)
3. Am I missing some functionality inside the graphql endpoints to Fix this?
From Native GraqpQl we cannot get the expected result.But we can write a custom module with the graphql and achieve it.What my suggestion is get the product detail by sku using _productRepository->get($sku);. This will return the entire product details.So you can get the attribute data by using $attributes = $_product->getAttributes(); Here is my data provider file.
/**
* #params string $sku
* this function return all the product data by product sku
**/
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
/**
* #params int $id
* this function return all the word of the day by id
**/
public function getAttributesBySku( $sku ){
$_product = $this->getProductBySku($sku);
$attributes = $_product->getAttributes();// All Product Attributes
$attributes_data = [];
$x=0;
foreach ($attributes as $attribute) {
if($attribute->getIsUserDefined()){ // Removed the system product attribute by checking the current attribute is user created
$attributeLabel = $attribute->getFrontend()->getLabel();
$attributeValue = $attribute->getFrontend()->getValue($_product);
if($attribute->getAttributeCode()=="language"){
$attributeLabelAndValue = $attributeLabel." - ".$attributeValue;
$attributes_data[$x]['atr_data'] = $attributeLabelAndValue;
}
}
$x++;
}
return $attributes_data;
}
This code will return the need
$attribute->getFrontend()->getLabel(); this will return the label and
$attribute->getFrontend()->getValue($_product); this will return the value.
To check the entire graphQl query and resolver file kindly view How to get product attribute value, label using GraphQl in Magento 2.3?
Hope this will help you.
You can try creating custom graphql attribute with custom resolver.
$product = $this->_productRepository->getById($value['entity_id']);
$data = array();
foreach ($args['fields'] as $fi) {
$att = $product->getResource()->getAttribute($fi);
if (isset($att) && $att) {
if (in_array(
$att->getFrontendInput(),
['multiselect', 'select']
)) {
$data[$fi . '_label'] = $product->getAttributeText($fi);
}
$data[$fi] = $product->getData($fi);
}
}
return json_encode((object) $data);
which should display all provided attributes with their labels.
"sku": "testProduct",
"fabric": 60,
"work": 65,
"dynamicAttributes": "{
"fabric_label":"Pure Silk",
"fabric":"60",
"work_label":"Tanchoi",
"work":"65"
}",
Check out entire module here

Displaying database columns based on current language

in database i have this:
name_en | name_fr
When the user select french language - for example - i want to get name_fr field, and the same thing if he chose another language
Assuming you set locale in your application using:
App::setLocale($lang);
if you use Eloquent, you can add to your model class accesssor:
public function getNameAttribute($value) {
return $this->{'name_'.App::getLocale()};
}
and also mutator:
public function setNameAttribute($value) {
$this->{'name_'.App::getLocale()} = $value;
}
Assuming you added those functions to Content model you can now use:
$content = Content::first(); // find first article
echo $content->name; // displaying its name
$content->name = 'updated content'; // changing its name
$content->save(); // saving
This will cause displaying and changing name_{$lang} if you set lang using setLocale

How to override save() method in a component

Where and how I am overriding the save method in Joomla 3.0 custom component ?
Current situation:
Custom administrator component.
I have a list view that displays all people stored in table.
Clicking on one entry I get to the detailed view where a form is loaded and it's fields can be edited.
On save, the values are stored in the database. This all works fine.However, ....
When hitting save I wish to modify a field before storing it into the database. How do I override the save function and where? I have been searching this forum and googled quiet a bit to find ways to implement this. Anyone who give me a simple example or point me into the right direction ?
Thanks.
Just adding this for anyone who wants to know the answer to the question itself - this works if you explicitly wish to override the save function. However, look at the actual solution of how to manipulate values!
You override it in the controller, like this:
/**
* save a record (and redirect to main page)
* #return void
*/
function save()
{
$model = $this->getModel('hello');
if ($model->store()) {
$msg = JText::_( 'Greeting Saved!' );
} else {
$msg = JText::_( 'Error Saving Greeting' );
}
// Check the table in so it can be edited.... we are done with it anyway
$link = 'index.php?option=com_hello';
$this->setRedirect($link, $msg);
}
More details here: Joomla Docs - Adding Backend Actions
The prepareTable in the model (as mentioned above) is intended for that (prepare and sanitise the table prior to saving). In case you want to us the ID, though, you should consider using the postSaveHook in the controller:
protected function postSaveHook($model, $validData) {
$item = $model->getItem();
$itemid = $item->get('id');
}
The postSaveHook is called after save is done, thus allowing for newly inserted ID's to be used.
You can use the prepareTable function in the model file (administrator/components/yourComponent/models/yourComponent.php)
protected function prepareTable($table)
{
$table->fieldname = newvalue;
}

Set dropdown input default value based on third parameter in Grocery CRUD

Code sample below,
function product($parameter){
$crud = new grocery_CRUD();
...
$crud->callback_add_field('dropdown_field_name',array($this,'_add_field_callback'));
...
$output = $crud->render();
}
Can I do something like this ?
function _add_field_callback($parameter){
//load db model
//call the result and return as dropdown input field with selected selection when value = $parameter
}
Actually this is easy to do it by using the controller. For example you can simply do:
function product($parameter){
$this->my_test_parameter = $parameter;
$crud = new grocery_CRUD();
...
$crud->callback_add_field('dropdown_field_name',array($this,'_add_field_callback'));
...
$output = $crud->render();
}
And the callback:
function _add_field_callback($parameter){
//load db model
//call the result and return as dropdown input field with selected selection when value = $parameter
$value = !empty($this->my_test_parameter) ? $this->my_test_parameter : '';
...
//here you can also use the form_dropdown of codeigniter (http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html)
}
I know that you are desperately looking forward for the default value for grocery CRUD so I added an issue to the github https://github.com/scoumbourdis/grocery-crud/issues/138 . This is will be a reminder that this thing has to be fixed.
I had implemented Grocery Crud in one of my web application.
Check this link on " How to create dependent dropdowns"
http://demo.edynamics.co.za/grocery_crud/index.php/examples/customers_management/add

Resources