I have to keep 2 fields(question,options) of questions table in encrypted form
core.php
Configure::write('Security.ekey', '1234567890~`!##$%^&*()_+-={}[]<>,./?qwertrpoiuytlkskhsffgzxmnxcbvLAKSJHFJPOQWUTERLKDJAD');
in Question modal i have
public $encryptedFields = array('question', 'options');
public function beforeSave($options = array()) {
foreach($this->encryptedFields as $fieldName){
if(!empty($this->data[$this->alias][$fieldName])){
$this->data[$this->alias][$fieldName] = Security::rijndael(
$this->data[$this->alias][$fieldName],
Configure::read('Security.ekey'), 'encrypt');
}
}
return true;
}
public function afterFind($results, $primary = false) {
//debug($results); this return nothing while data is being displayed having question and option fields null
foreach($this->encryptedFields as $fieldName){
if(!empty($results[$this->alias][$fieldName])){
$results[$this->alias][$fieldName] = Security::rijndael(
$results[$this->alias][$fieldName],
Configure::read('Security.ekey'), 'decrypt');
}
}
return $results;
}
I have searched different blogs and stackoverflow questions
also tried changing datatype of fields to (text,blob,longblob)
length of these fields may be long (equalent to text datatype) please also suggest which datatype will be best to keep its encrypted string.
Related
Post.php
$fillable = ['id','flag'];
public function tags()
{
return $this->belongsToMany('App\Tags')->withPivot('pivot_flag');
}
public function flaggedTags()
{
return $this->tags()->where('pivot_flag', 1)->get();
}
Tag.php
$fillable = ['id']
public function posts()
{
return $this->belongsToMany('App\Post');
}
Pivot table post_tag columns: post_id,tag_id,pivot_flag
I need to fetch Post with Tags:
$post = Post::select('id','flag')->with('tags')->find($postId);
However, if the flag is set to 1 in Post then I only want tags which have pivot_flag value in pivot table post_tag set to 1
So in that case I'd have following:
$post = Post::select('id','flag')->with('flaggedTags')->find($postId);
How can I do this in single query? I could always do one query to check if the Post is flagged or not and then run the appropriate query for Tags, but that seems wasteful.
Eloquent or raw MySQL queries are welcome.
Thanks!
UPDATE - Alternative solution
Load all tags and then filter the result depending on Post's flag value:
if($post->flag)
{
$tags = $post->tags->filter(funtction($q) {
return $q->pivot->pivot_flag == 1;
});
}
else
{
$tags = $post->tags;
}
This might be faster depending on number of tags. Not really sure, need to test it though. Any feedback about this in comments is welcome.
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.
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 :)
I am having a weird issue. I have a Domain object:
class MyClass {
String name
Boolean new = true
String number
String type
Byte[] data
Date dateCreated
Date lastUpdated
static belongsTo = [
other: MyOtherClass
]
static mapping = {
table 'my_classes'
data column: "data", sqlType: "MEDIUMBLOB"
}
static constraints = {
data maxSize: 8000 * 66
number nullable: true
}
}
In the Controller I have (edited to show entire method):
def list = {
def myOtherClasses = MyOtherClass.getAll()
if ( !params.max ) params.max = 20
if ( !params.sort && !params.order ) {
params.sort = "new"
params.order= "desc"
}
def myClassCount = MyClass.createCriteria().count() {
'in'( 'other', myOtherClasses )
order( params.sort, params.order )
}
def myClassList = MyClass.createCriteria().list() {
'in'( 'other', myOtherClasses )
order( params.sort, params.order )
}
return [ myClassList: myClassList, myClassCount: myClassCount ]
}
The result if fine and the view is correct. But each time this code runs, the data property isDirty, so version is incremented, and lastUpdated is updated.
The data property is holding audio data, but I don't think that is relevant.
I can't figure out what is going on here. So my question is, how do I make it stop updating?
Using:
Grails 2.4.4
Hibernate 3.6.10.18
MySQL 5.7.9
Thanks in advance :)
After much research and testing, and a few great articles, I have found a solution:
Instead of using type Byte[] in the Domain Object, I use java.sql.Blob, and removed the sqlType in the mapping.
In the controller, I had to make a few changes to access the Byte[] data from the Blob, but that was easy.
I still don't know why this was happening, and I couldn't find any info on it, but it is working as expected now.
I use the following code to insert a new record to my Users table:
public bool CreateUser(User obj)
{
obj.Id = Guid.NewGuid();
using (_db = new CMSDataContext())
{
obj.SiteId = SiteID;
_db.Users.InsertOnSubmit(obj);
_db.SubmitChanges();
}
return true;
}
I do not get any errors, and everything seems fine. I can read a record from database with same DataContext. But after the above method runs completely, I see nothing new in my Users table. Why?
Is the id column truly a PK in the sql server database?