Time column value change without touching it - mysql

I have a "Trip" model that has "time" column that represents the trip time (in addition to the created_at and updated_at) columns. There's a hasOne relation between the "Trip" model and a "Bus" model (every bus can be associated to more than 1 trip).
I want to set the "bus_id" column in the "Trips" table to null on deleting the corresponding bus.
When I do so, something very strange happens, the "time" column in the "Trips" table changes to the current time (similar to updated_at).
Note:
This happens only in this case; any update to the "Trip" table doesn't change the column.
When I inspect the code with dd(), I find the "time" column unchanged, but then the value is changed in the database.
Trip Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
//
protected $fillable = ['time', 'bus_id'];
public function getStationsStringified(){
$stations = $this->stations()->orderBy('order', 'asc')->pluck('name')->toArray();
return implode(",", $stations);
}
public function bus(){
return $this->belongsTo(Bus::class);
}
public function tickets(){
return $this->hasMany(Ticket::class);
}
public function stations(){
return $this->belongsToMany(Station::class)->withPivot("order");
}
}
Bus Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Bus extends Model
{
//
protected $fillable = ['license_num'];
public function trips(){
return $this->hasMany(Trip::class);
}
}
Destroy function in BusController:
public function destroy($id){
$bus = Bus::find($id);
foreach ($bus->trips as $trip){
if($trip->time < Carbon::now()->startOfDay()){
$trip->bus()->dissociate();
$trip->save();
}else{
return new Response("There a trip ".$trip->getStationsStringified()." associated with this bus, please change its bus before deletion", Response::HTTP_CONFLICT);
}
}
$bus->delete();
return new Response("Deleted", Response::HTTP_OK);
}

The problem was that I used timestamp() in the migrationn file defining the trips table, which adds automatically extra function on the time column set to current time on update.
The reason why the problem doesn't happen on any other updates is that I was setting a value to the time column.
The solution is to use datetime() in the migration instead of timestamp().

Related

Accessing to the `latest` record stored in DB table right after recording it

In Laravel After recording last row to a DB table, can I safely access same recorded data right after recording it by calling latest() queries? Because transactions by other users may occur at the same time, and it may not really be the last record anymore?
Edit:
For example:
Public function StoreNotif($data){
auth()->user()->Notif()->create(store $data here..)
}
Public function SendNotif(){
$data="123";
$this->StoreNotif($data)
event(new Notification(stored Notif instance?));
}
No, you cannot rely on the database to return the record from your current script.
The ->latest() method will always sort the records with the most recent created_at date first.
https://laravel.com/docs/6.x/queries#ordering-grouping-limit-and-offset
But you haven't provided any code or explanation as to why this is a concern. If you just created a new record, why do you need to query it again? You should already have access to an instance of the model.
EDIT: I've made a few edits to demonstrate how you would pass the model from a controller to an event as referenced in the comments. Please post your code if you want more specific help.
SomeController.php
function store()
{
$model = Model::create([
'some_data' => 1
]);
// fire an event with the newly created model
event(new SomeEvent($model));
dd($model);
}
------------------------
Model {
// ...
attributes: [
'id' => 101,
'some_data' => 1
'created_at' => '2019-10-06 12:48:01',
'updated_at' => '2019-10-06 12:48:01',
]
// ...
}
SomeEvent.php
<?php
namespace App\Events;
use App\Model;
use Illuminate\Queue\SerializesModels;
class SomeEvent
{
use SerializesModels;
public $model;
public function __construct(Model $model)
{
$this->model = $model;
// ...
}
}
EDIT: Per your newly added code, you just need to pass the new model back to the original method. You could do something like this.
Public function StoreNotif($data)
{
// add a return statement
return auth()->user()->Notif()->create(store $data here..);
}
Public function SendNotif()
{
$data="123";
// store the returned data to a variable
$model = $this->StoreNotif($data);
// call the event with the model instance
event(new Notification(model));
}
I'm not sure what 'latest' is but I do know that MySQL uses SELECT LAST_INSERT_ID as the query to get the 'per-connection' id of the last inserted item. Under the covers it's using mysql_insert_id so if you are in a language that supports it, you could use that too.

Joining Two Tables to a Reference Table Laravel

I Have three tables
#1 Table timeline which is my reference table with an Auto incremented ID which is stored in column id
#2 timeline_videos
#3 timeline_else
What happens is on post if a video is uploaded with the post
it will go into Table #2 ,anything else goes into Table #3.
#2-3 have the Auto Increment Id from the Table timeline stored in a column pid
On query of The Timeline I need to join both tables data using id=pid
so I can use the rest of the Relational Data with the post.
I have done a bit of research and can't seem to find a method for doing so.
So far the code I have
Controller
$groupposts = timeline::where([
['owner','=',$owner],['id','<',$lastid],
])
->join('timeline_videos','timeline.id','=','timeline_videos.pid')
//->join('timeline_else','timeline.id','=','timeline_else.pid')
->orderBy('id','desc')
->limit(5)
->get();
This works with no errors with the second Join commented out but I need to also grab the timeline_else data .
Update --
I have now decided to use Eloquent Relationships to join the tables,
my question now is what type of relationship do I have between the
tables?
I realize it basically needs to be able to switch between two tables to
grab data based on the fact that timeline_videos and timeline_else will not be "JOIN" but separated by type .
The tables need to Join with table #1 timeline based on a column I now have named type for clarifying where to look and matching/joining using the id = pid
You can use relationships.
it sounds like timelines has many videos and has many video failures
https://laravel.com/docs/5.5/eloquent-relationships#one-to-many
you would have a model for each table and set up the relationships
timelines model:
public function videos()
{
return $this-> hasMany('App\Videos');
}
public function videoFailures()
{
return $this-> hasMany('App\videoFailures');
}
videos model:
public function timeline()
{
return $this->belongsTo('App\Timelines');
}
videos failures model:
public function timeline()
{
return $this->belongsTo('App\Timelines');
}
You can then go:
$timeLine = Timmeline::find($id);
to find videos of the time lines you would do:
$videos = $timeLine->videos();
to find else:
$videoElse = $timeLine-> videoFailures();
By using some of what Parker Dell supplied and a bit more trial and error
My Models Looks like
timeline
class timeline extends Model
{
protected $table ='timeline';
public $timestamps = false;
public function videos()
{
return $this->hasMany('App\timeline_videos','pid','id');
}
public function else()
{
return $this->hasMany('App\timeline_ect','pid','id');
}
}
timeline_ect.php ,I changed the name of the table
class timeline_ect extends Model
{
protected $table='timeline_ect';
public $timestamps = false;
public function timeline()
{
return $this->belongsTo('App\Models\timeline','pid','id');
}
}
timeline_videos
class timeline_videos extends Model
{
protected $table='timeline_videos';
public $timestamps = false;
public function timeline()
{
return $this->belongsTo('App\timeline','id','pid');
}
}
Then Lastly my Controller
$timeline = timeline::with('videos','else')
->orderBy('id','desc')
->limit(5)
->get();
So far no Problem query is correct.

Insert a field with value whenever new instance of modal is created

I want to insert a field with value whenever new row is created for a modal.
Ex: Suppose this is my user.php modal
class User extends Authenticatable
{
protected $guarded = ['id'];
}
What i want is in my application anywhere when i insert a row in user table, then i want to insert an extra column code with its value in user table.
Ex: If i do below in my application
User::create(['name'=>'xyz', 'password' => 'kajsndjk']);
then it should insert an extra column code =>'Qwedf' also in my table.
In my application there are many places where i am creating the users, so i don't want to remember every time to insert code column.
Please suggest how can i achieve it.
Overriding the static create function on the User class is the only thing that will work in my opinion.
public static function create(array $attributes = [])
{
$object = parent::create($attributes);
$object->code = 'some text';
$object->save();
return $object;
}
I've tested and like I expected, oseintow's answer will not work, because it would work only if you directly modified code variable, which you obviously are not doing.
Add this mutator to your User model
public function setCodeAttribute($value)
{
$this->attributes['code'] = "Qwedf";
}
Anytime you are saving a record code will be assigned the Qwedf value

Getting Form Data in edit-view with JTable which is joined?

i'm stuck with a problem of joined tables and retrieving Form-Data how it "should be" in best-practise-terms of Joomla!
I'm following the Joomla!-standards as far as my knownledge reaches, and the goal is to write this component "as if" it was native Joomla!-Code.
So what i have is the following COM_COMPONENT\models\release.php
<?php
defined('_JEXEC') or die;
class DojoMusicLibraryModelRelease extends JModelAdmin
{
public function getTable( $type = 'Releases', $prefix = 'DojoMusicLibraryTable', $config = array() )
{
return JTable::getInstance($type, $prefix, $config);
}
public function getForm( $data = array(), $loadData = true )
{
$options = array('control' => 'jform', 'load_data' => $loadData);
$form = $this->loadForm('releases', 'release', $options);
if (empty($form)) {
return false;
}
return $form;
}
protected function loadFormData()
{
$app = JFactory::getApplication();
$data = $app->getUserState('com_dojomusiclibrary.edit.release.data', array());
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
}
And in COM_COMPONENT\tables\releases.php
<?php
defined('_JEXEC') or die;
class DojoMusiclibraryTableReleases extends JTable
{
public $id;
public $title;
public $alias;
public $artist_id;
public $release_date_digital;
public $release_date_physical;
public $ean;
public $catalog_number;
public $promotional_text;
public $is_compilation;
public $format_id;
public $release_status;
// TODO: This tracklist should not be in this table, but only an id referring to it
public $tracklist;
public $created_at;
public $modified_at;
public $state;
public $publish_up;
public $publish_down;
public function __construct($db)
{
parent::__construct('#__dojomusiclibrary_releases', 'id', $db);
}
}
So now, as you can see from the comment in the latter code-example, the variable $tracklist is a field in the releases-table in my MySQL by now. As i got a "repeatable"-field-type, there is JSON inside of that field, and it works so far.
But the component is meant to hold another MySQL-table called "tracks", which holds all tracks of all releases and should be joined by a tracklist-id to the releases table, so that we have the following three tables:
releases (holds all the data, that is strictly bound to a single release/album/ep...)
tracklists (is an 1-to-m relation table, that has a tracklist_id, which is joined with the release and joins all single track_ids that belong to the tracklist)
tracks (holds all the track data, such as track_title, duration, genre and so on, while every track has an unique id, which can be joined to the tracklists)
As you can see this is getting more and more complex (especially if you consider that this is not the only part of the component, where this kind of joining tables for a single JForm will be needed).
Excerpt from the COM_COMPONENT\models\forms\release.xml
<!-- CATALOG NUMBER -->
<field name="catalog_number" type="text"
label="COM_DOJOMUSICLIBRARY_FORM_FIELD_CATALOG_NUMBER_LABEL"
description="COM_DOJOMUSICLIBRARY_FORM_FIELD_CATALOG_NUMBER_DESC" />
So now, as JForm seemingly expects something coming from the JTable since the release.xml binds the field-name to the naming of the variable in the JTable-Class, i do not really know how to deal with that, given that the data, that should be passed in one Form is coming from different tables.
So in summary i got different problems as far as i can see:
How can i manage to join tables for (best practice and right)
treatment of the JForm Standard in Joomla! ?
Since i use a repeatable-field-type to manage the tracklist, data will be stored into a JSON and saved to the database in only one field.
I need this repeatable solution since every release has n tracks with more than one information (track_no, title, genre...) and thanks to Joomla there is finally a native way to handle such cases.
BUT: Before saving them to the database the JSON must be split up into it's single values and be assigned to the proper fields in the tracks-table.
Okay... I know this is a huuuuuge question maybe... but since i'm totally stuck, I'd be happy about any advice for at least one of the issues :D
Thanks in advance :)

Yii model: Dynamic table relations

Table.linkedIndex is related to LinkedIndex.ID. The value of the field LinkedIndex.TableName is either Linked1 or Linked2 and defines which of these tables is related to a row in Table.
Now i want to make a dynamical link with Yii models so that i can easily get from a Table row to the corresponding Linked1 or Linked2 row:
Table.linkedID = [LinkedIndex.TableName].ID
Example
Table values:
LinkedIndex values:
Now I should get the row from Linked2 where ID=2:
$model = Table::model()->findByPk(0);
$row = $model->linked;
Model
In the model Table, I tried to make the relation to the table with the name of the value of linkedIndex.TableName:
public function relations()
{
return array(
'linkedIndex' => array(self::HAS_ONE, 'LinkedIndex', array('ID' => 'linkedIndex')),
'linked' => array(
self::HAS_ONE,
'linkedIndex.TableName',
array('ID' => 'linkedID'),
)
)
}
But then I get the error:
include(linkedIndex.TableName.php) [function.include]: failed to open stream: No such file or directory
Is there any way to make a dynamic relation Table.linkedID -> [LinkedIndex.TableName].ID with Yii Models?
Per the Yii docs here:
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#relations-detail
I'd suggest using self::HAS_ONE instead (unless there can be multiple rows in LinkedIndex with the same ID - although from the looks of above, I doubt that's the case).
You can link tables together that have different keys by following the schema:
foreign_key => primary_key
In case you need to specify custom PK->FK association you can define it as array('fk'=>'pk'). For composite keys it will be array('fk_c1'=>'pk_с1','fk_c2'=>'pk_c2').
so in your case:
public function relations(){
return array(
'linkedIndex' => array(self::HAS_ONE, 'LinkedIndex', array('ID' => 'linkedIndex')),
);
}
where LinkedIndex is the class name for the LinkedIndex model (relative to your Table model - i.e. same folder. You could change that, of course) and array('ID' => 'linkedIndex') specifies the relationship as LinkedIndex.ID = Table.linkedIndex.
Edit
Looking at your updated example, I think you're misunderstanding how the relations function works. You're getting the error
include(linkedIndex.TableName.php) [function.include]: failed to open stream: No such file or directory
because you're trying to create another relation here:
'linked' => array(
self::BELONGS_TO,
'linkedIndex.TableName',
array('ID' => 'linkedID'),
)
This part: linkedIndex.TableName refers to a new model class linkedIndex.TableName, so Yii attempts to load that class' file linkedIndex.TableName.php and throws an error since it doesn't exist.
I think what you're looking for is to be able to access the value TableName within the table LinkedIndex, correct? If so, that's accessible from within the Table model via:
$this->linkedIndex->TableName
This is made possible by the relation we set up above. $this refers to the Table model, linkedIndex refers to the LinkedIndex relation we made above, and TableName is an attribute of that LinkedIndex model.
Edit 2
Per your comments, it looks like you're trying to make a more complex relationship. I'll be honest that this isn't really the way you should be using linking tables (ideally you should have a linking table between two tables, not a linking table that says which 3rd table to link to) but I'll try and answer your question as best as possible within Yii.
Ideally, this relationship should be made from within the LinkedIndex model, since that's where the relationship lies.
Since you're using the table name as the linking factor, you'll need to create a way to dynamically pass in the table you want to use after the record is found.
You can use the LinkedIndex model's afterFind function to create the secondary link after the model is created within Yii, and instantiate the new linked model there.
Something like this for your LinkedIndex model:
class LinkedIndex extends CActiveRecord{
public $linked;
public static function model($className = __CLASS__){
return parent::model($className);
}
public function tableName(){
return 'LinkedIndex';
}
public function afterFind(){
$this->linked = new Linked($this->TableName);
parent::afterFind();
}
//...etc.
}
The afterFind instantiates a new Linked model, and passes in the table name to use. That allows us to do something like this from within the Linked model:
class Linked extends CActiveRecord{
private $table_name;
public function __construct($table_name){
$this->table_name = $table_name;
}
public static function model($className = __CLASS__){
return parent::model($className);
}
public function tableName(){
return $this->table_name;
}
//...etc.
}
which is how we dynamically create a class with interchangeable table names. Of course, this fails of the classes need to have separate operations done per-method, but you could check what the table_name is and act accordingly (that's pretty janky, but would work).
All of this would result in being to access a property of the linked table via (from within the Table model):
$this->linkedIndex->linked->foo;
Because the value of LinkedIndex.TableName and Table.linkedID is needed to get the values, I moved the afterFind, suggested by M Sost, directly into the Table-Class and changed its content accordingly. No more need for a virtual model.
class Table extends CActiveRecord {
public $linked; // Needs to be public, to be accessible
// ...etc.
public function afterFind() {
$model = new $this->linkedIndex->TableName;
$this->linked = $model::model()->findByPk( $this->linkedID );
parent::afterFind();
}
// ...
}
Now I get the row from Linked2 where ID=2:
$model = Table::model()->findByPk(0);
$row = $model->linked;