RedBean PHP credit card number issue - mysql

I have started using Redbean PHP recently. So I am not much aware of how it deals things.
Until now I love how simple it is making things for me. But I have ran into quite an issue today. I need to store credit card numbers into a table. But as soon as I store the bean, the card number gets changed into a float(decimal) kind of value.
'1234123412341234' is getting stored as '1.234123412341234e15'
The datatype is 'double' created by redbean, but I gave as a string. This is kind of weird for me I am not much of an expert in either SQL or PHP. Is there a way to override how redbean creates table. So someone please help me. Am I missing something here. The following is my corresponding code and the framework used is Codeigniter.
Data Variable
$data = array(
'card_name' => 'Shiva Kumar Avula',
'card_no' => '1234123412341234',
'card_issuer' => 'Visa',
'card_cvv' => '123',
'card_exp_month' => 12,
'card_exp_year' => 2020
);
$card = $this->card_model->create_card($data, TRUE); // Making it primary
Model Function
public function create_card($data, $is_primary = FALSE)
{
$card = R::dispense('card');
$card->name = $data['card_name'];
$card->number = $data['card_no'];
$card->issuer = $data['card_issuer'];
$card->cvv = $data['card_cvv'];
$card->exp_month = $data['card_exp_month'];
$card->exp_year = $data['card_exp_year'];
$card->is_primary = $is_primary;
$card->is_verified = 0;
$card->ts_created = $this->ts_sql;
$card->ts_modified = $this->ts_sql;
$id = R::store($card);
}
Snapshot of my output in phpmyadmin,
Snapshot that shows the datatype,

You can set the beans meta property for number to string.
$card->setMeta("cast.number", "string");
This will save any values of $card->number as varchar.
See RedBean Internals for more information.

Related

cakephp 3 save data to database dont work after adding new column

Hey i want to save data to my database with cakephp 3.8. All works but i added a new field "created_by" and changed it in the models folder.
this is my RequestTable.php
public function validationDefault(Validator $validator){
.... (more, but not important)
$validator
->scalar('created_by')
->maxLength('created_by', 100)
->allowEmptyString('created_by');
return $validator;
}
My entity file "Request.php"
in the top
* #property string|null $created_by
and
protected $_accessible = [
... (more)
'created_by' => true,
];
My code where i want to save the data:
$request = $this->Requests->newEntity();
$session = $this->getRequest()->getSession();
if(!empty($session->read('Auth.User.username'))){
$this->request->data['Requests']['created_by'] = $session->read('Auth.User.username');
}
$request = $this->Requests->patchEntity($request, $this->request->getData('Requests'));
if ($result = $this->Requests->save($request)) {
...
}
At the empty check it goes in the clause. After the patchEntity the result is the correct data. The same in the save.
The column in the table looks like
created_by varchar(100) DEFAULT NULL
I dont know why it doesnt save the data. If someone have more questions about the code please ask :)
Without an error message. It would be hard to determine where is the flaw. One thing you might consider is checking the database if it is busy handling another process. If there are flood requests that makes your request locked.
I dont know why, but if I create a new database with the same schema it works. Thank you all for your help :)

putting if statement in api with laravel

I'm currently learning on how API works and my mentor gave me a task to create submit the form using API and store the data on database with laravel and the task also require mandatory if logic on some field.
I have succeeded with the first task (storing data to the database ) and I'm having difficulties writing the mandatory if.
I'm confused, on my task paper I'm told to create one controller, one model and two endpoints (request-schedule and request-leaving) where each endpoint should have some parameters.
and for the request-leaving parameter, there are 10 parameters, 6 of them have this requirement like (mandatory if request type Request Day Off)
There are 3 requests typewritten on there
1. Request Day off
2. Request Schedule
3. Change Schedule
I'm a super newbie in programming, does anyone know how to solve this?
public function CreateReqSchedule(Request $request)
{
$reqschedule = new B777();
$reqschedule->reqtype = $request->input('reqtype');
$reqschedule->startdate = $request->input('startdate');
$reqschedule->enddate = $request->input('enddate');
$reqschedule->reason = $request->input('reason');
$reqschedule->route = $request->input('route');
$reqschedule->actualschedule = $request->input('actualschedule');
$reqschedule->changetoschedule = $request->input('changetoschedule');
$reqschedule->swapcrewid = $request->input('swapcrewid');
$reqschedule->swapcrewschedule = $request->input('swapcrewschedule');
$reqschedule->note = $request->input('note');
$reqschedule->save();
return response()->json($reqschedule);
}
code above is my only work, I'm feeling anxious, because I've googled it myself but I'm still stuck.
So you are talking about validations. you can put laravel validation into it like below
Use required validation for mandotory data and return response in json
// Making validation for fields
$validator = \Validator::make($request->all(), [
'fields1' => 'required',
'fields2' => 'required',
'fields3' => 'required',
]);
if ($validator->fails())
{
// return response on validaton fails
return response()->json(['status'=>400,'errors'=>$validator->errors()->all()]);
}
// If validation passes store your data in database

How can I have the name of my entity instead of the id in the related tables

I'm creating a project on CakePHP 3.x where I'm quite new. I'm having trouble with the hasMany related tables to get the name of my entities instead of their ids.
I'm coming from CakePHP 2.x where I used an App::import('controller', array('Users') but in the view to retrieve all data to display instead of the ids, which is said to be a bad practice. And I wouldn't like to have any code violation in my new code. Can anybody help me? here is the code :
public function view($id = null)
{
$this->loadModel('Users');
$relatedUser = $this->Users->find()
->select(['Users.id', 'Users.email'])
->where(['Users.id'=>$id]);
$program = $this->Programs->get($id, [
'contain' => ['Users', 'ProgramSteps', 'Workshops']
]);
$this->set(compact('program', 'users'));
$this->set('_serialize', ['ast', 'relatedUser']);
}
I expect to get the user's email in the relatedUsers of the program table but the actual output is:
Notice (8): Trying to get property 'user_email' of non-object [APP/Template\Asts\view.ctp, line 601].
Really need help
Thank you in advance.
You've asked it to serialize the relatedUser variable, but that's for JSON and XML views. You haven't actually set the relatedUser variable for the view:
$this->set(compact('program', 'users', 'relatedUser'));
Also, you're setting the $users variable here, but it's never been initialized.
In addition to #Greg's answers, the variable $relateduser is still a query object, meaning that trying to access the email property will fail. The query still needs to be executed first.
You can change the query to:
$relatedUser = $this->Users->find()
->select(['Users.id', 'Users.email'])
->where(['Users.id' => $id])
->first();
Now the query is executed and the only the first entry is returned.
There is are a number of ways to get a query to execute, a lot of them are implicit is use. See:
Cookbook > Retrieving Data & Results Sets

GetStream Laravel - Cannot batchadd to notification

I can't seem to use the batcher to copy an activity to notifications. I read from the docs
that there is a 100 limit to the TO field for copying activities, so i tried out using the batcher. but it does'nt seem to work. did I miss out something on the docs or on my code? if so, how do I get over the 100 limit?
$evtConvMention = array();
$evtConvMention[] = "event:{$event->id}";
$evtConvMention[] = "notification:1";
$evtConvMention[] = "notification:2";
$batcher = FeedManager::getClient()->batcher();
$batcher->addToMany([
"actor" => "App\User:{$user->id}",
"verb" => "eventpost",
"object" => "App\EventConversation:{$post->id}",
"foreign_id" => "App\EventConversation:{$post->id}"
], $evtConvMention);
The addToMany() call will have a similar limit. While I look into the PHP library a little more, it might be easier to use the To field in the activity payload itself.
$feed = FeedManager::getClient()->feed("event", $event->id);
$now = new \DateTime("now", new \DateTimeZone('Pacific/Nauru'));
$data = [
"actor" => "App\User:{$user->id}",
"verb" => "eventpost",
"object" => "App\EventConversation:{$post->id}",
"foreign_id" => "App\EventConversation:{$post->id}",
"time" => $now
];
$feed->addActivity($data);
We also HIGHLY recommend sending your own foreign_id and time fields in the payload as well (I've added an idea for the $now value in the code above, otherwise every feed will get its own unique record, which are a limited resource on your account.
If you have more than 100 notification feeds to write this into, it might be better to have the notification feeds for those users 'follow' the event feed. Then you don't need to use the to field at all.

How to generate a list of unique years between several dates in Lumen/Laravel

i'm creating an API in Lumen and i need to create a method that will get dates from two colums on the same table and return any years that occur between those dates, returning them all as a single array.
So for instance, imagine a table column named start_date and another named end_date
start_date | end_date getAllYears() should return =>
[1983, 1984, 1985, 1986, 1987,
1999, 2000, 2001, 2002, 2003,
..., 2016]
1999-05-09 | 2002-04-03
1983-03-12 | 1987-09-23
2001-02-12 | 2016-11-27
Currently i have a method that manages to do this on other types of more specific queries, the major problem with this attempt, is that due to the sheer mass of SQL records that i'm retrieving, a method like this causes my request to time out every single time.
MY INEFFICIENT METHOD that makes little use of Lumen/Laravel
public function getAllYears(){
$dates = $this->model->select('data_ini', 'data_fim')->get();
$results = [];
foreach ($dates as $obj){
$carbonBegin = Carbon::createFromDate($obj->data_ini->year);
$carbonEnd = Carbon::createFromDate($obj->data_fim->year);
if($carbonEnd->year === 9999){
$carbonEnd->year = date('Y');
}
$carbonEnd->year++;
// Simple method that runs a DatePeriod method
$dateRange = $this->helper->createDateRange($carbonBegin, $carbonEnd);
$results = array_merge($results, $dateRange);
}
sort($results);
$cleanYears = array_unique($results);
if ($cleanYears == null)
return response()->json(['error' => true, 'errorCode' => '1008', 'message' => "No years found!"]);
else
return response()->json(['error' => false, 'years' => $cleanYears]);
}
So, the question is, how can i do this in a less expensive way so that my server doesn't time out on every request? Thank in advance for your help :)
NOTE: DB:raw is a no-go as my TL has forbidden me from using it anywhere on the API
Looks like you need the whereBetween:
$between = DB::table('theTable')->whereBetween('data_ini', ["str_to_date('2011-05-06','%Y-%m-%d')", "str_to_date('2011-05-06','%Y-%m-%d')"])->get();
With models:
$between = $this->model->whereBetween('data_ini', ["str_to_date('2011-05-06','%Y-%m-%d')", "str_to_date('2011-05-06','%Y-%m-%d')"])->get();
In the above, I am utilizing MySQL's built-in str_to_date
Hope this helps!