How to edit Google Calender event using Zend_Gdata_Calendar class ? - php

When you retrieve data from a calendar you can set the user before the data is retrieved like this
$gdataCal = new Zend_Gdata_Calendar($this->client, $this->domain);
$calendar_list = $gdataCal->getCalendarListFeed();
$query = $gdataCal->newEventQuery();
$query->setUser( $username . '%40domain.com');
But how can you do this when you want to create/update an event?
$service = new Zend_Gdata_Calendar($this->client, $this->domain);
$event = $service->newEventEntry();
There are no methods like setUser on any of the objects returned

I have been into similar condition but, the wordaround that I did was :
$service->delete($event->getEditLink()-><URL-Of-The-Event>);
and later, create another one.
However, that was approximately an year ago. I am not sure if any new functions have come up because, I never needed it after that.
If no one answers, this wordaround has to work for you :)

Related

Concentrate text not function properly (Yii2 framework)

I want to concentrate some text from another table and put it in a new field.
$model1 = new CourseDetails();
$model1->course_shortform= CourseDetails::find()->select('course_shortform')->where(['course_name'=>$model->course_name]);
And then using the code below I was able to get the value from another table to fill in the field like so:
$model->intake_no = $model1->course_shortform;
Its all going well until i wanted to add some text for the intake no column:
$model->intake_no = $model1->course_shortform . "This is new text" ;
The system didn't display errors but it will show the text like this:
I wonder if got some alternative method to concentrate the text but I can't figure it out. I will glad if someone can help.
You are creating new instance of CourseDetails model then you create a query (instance of ActiveQuery) for selecting course_shortform and assigning that query into course_shortform property in that code of yours.
Instead of that you should skipt this line:
$model1 = new CourseDetails();
And you should use your query to find one model like this:
$model1 = CourseDetails::find()
->select('course_shortform')
->where(['course_name'=>$model->course_name])
->one();
Or if you need to create instance for $model1 by yourself you should use scalar() method to get the value returned by select directly.
$model1 = new CourseDetails();
$model1->course_shortform = CourseDetails::find()
->select('course_shortform')
->where(['course_name'=>$model->course_name])
->scalar();
You miss the fecth function .. for a model should be ->one()
$model1 = new CourseDetails();
$model1= CourseDetails::find()
->select('course_shortform')
->where(['course_name'=>$model->course_name])
->one();
and you should not assign the model to text field var so you don't $model1->course_shortform but on $model1

easiest way to fetch query to array of object in symfony

View in database I mean :
create view `vMaketType` as select * from MaketType
I have a view in database, but because of doctrine cant support it now, i using query, and fetch it one by one :
$em = $this->getDoctrine()->getManager();
$con = $this->getDoctrine()->getEntityManager()->getConnection();
$stmt = $con->executeQuery('SELECT * FROM vMaketType');
$domain = [];
//I must fetch it and set it one by one
foreach ($stmt->fetchAll() as $row){
$obj = new vMaketType();
$obj->setId($row["Id"]);
$obj->setName($row["Name"]);
$obj->setAmount($row["Amount"]);
array_push($domain, $obj);
}
for me this is really takes too much time to code one by one.
vMaketType is a custom entity I created to send data from controller to [Twig]view.
is there any easier way to fetch to array of object vMaketType?
because I have a view with 24 fields, I wish there is easier way for it.
Perhaps you can try with the serializer:
$obj = $this->get('serializer')->deserialize($row, 'Namespace\MaketType', 'array');
Code not tested, tweaks may be done, see the related doc.

What's a good way to have a PHP class mirror SQL table?

I want to learn PHP and have chosen to make a simple project tracker as an exercise.
I can't find a nice (DRY) way to map my model class to the sql row.
Right now I have a global $TasksColumns and always check that indexes are present:
$TasksColumnId = "Id";
$TasksColumnState = "State";
$TasksColumns = [
$TasksColumnId,
"Summary",
"Description",
$TasksColumnState,
"Estimation",//EstimateDuration
"Actual",//Duration
"FixVersion"
// Add Rank
// Add Priority
];
And then manually check in code:
$task -> Id = $task -> valueIfExistsKeyInArray("Id", $IdSummaryDescriptionStateEstimationActualFixVer);
$task -> Summary = $task -> valueIfExistsKeyInArray("Summary", $IdSummaryDescriptionStateEstimationActualFixVer);
What is a better way to accomplish this? (So I won't have to touch all the code when adding a new SQL column)
Thanks
You could use Doctrine.
It's a framework for mapping database rows to php classes.
Here on wikipedia are some examples. This example is copied from wikipedia:
$user = new User();
$user->name = "john1";
$user->password = "doe";
$entityManager->persist($user);
$entityManager->flush();
echo "The user with id $user->id has been saved.";
Here is the Doctrine tutorial. Retrieving objects is also easy (example from the tutorial) :
$reporter = $entityManager->find("User", $theReporterId);
But you can also perform more complex queries for retrieving objects.

Duplicate an AR record & re-insert this into the database

I have a AR model that I am trying to duplicated but just need to manually change the foreign key.
$_POST['competition_id'] = 99;
$prizes = CompetitionPrizes::model()->findAll('competition_id =:competition_id',array(':competition_id'=> $_POST['competition_id']));
This query basically queries the prizes table and gets all the rows for a particular competition. With the prizes object I would like to basically re-insert/duplicate the same information except the competition id which I want to manually set.
I did something similar for an AR object that basically only has one row and that worked well, however in this instance as a competition can have more than one prize this same code won't.
// My existing code for duplication process
$obj = Competitions::model()->find('competition_id=:competition_id', array(':competition_id' => $post['competition_id']));
$clone = clone $obj;
$clone->isNewRecord = true;
unset($clone->competition_id); // i want to remove this so it is auto inserted instead via the db
$clone->save();
This works great - how would I modify this on a 'collection' of prizes and have this duplicated into the database while setting my own 'competition_id' value.
Note - i'm to new to Yii, so please let me know if I have made any obvious errors/bad practice
Cloning won't work. You need to assign the attributes to a new object:
$obj = Competitions::model()->find('competition_id=:competition_id', array(':competition_id' => $post['competition_id']));
$clone = new Competitions;
$clone->attributes = $obj->attributes;
$clone->save();
If a more generic way of duplicating a Model / ActiveRecord in Yii2 Framework is required, you might use this solution:
$copy = clone $model;
$copy->isNewRecord = true;
foreach ($model->getPrimaryKey(true) as $field => $value) {
unset($copy->{$field});
}
$copy->save();
GitHub issue discussion about duplicate models: https://github.com/yiisoft/yii2/issues/7544#issuecomment-77158479
The answer for my problem although Michiel above helped me out - alternatively if you wouldn't mind adding another answer i'll give you the accepted answer.
foreach($models as $model)
{
$clone = new Competitions;
$clone->attributes = $model->attributes;
$clone->competition_id = '123' // custom var i am setting manually.
$clone->save();
}
How about (yii2 syntax):
$model=Competitions::findOne([':competition_id' => $post['competition_id']]);
$model->id = null;
$model->isNewRecord = true;
$model->save();

Zend Framework and Preventing Fat Controllers

Avoiding Fat Controller
So I'm using Zend Framework and I have a question involving preventing fat controllers with one of my actions. Basically I am normalizing a CSV file into my database.
This means that I have to get the feed and then use my model.
The feed grabbing is just there to show how it works, but that is now an Action Helper.
I am using the Data Mapper pattern with Zend Framework. I hate that I am doing this in my Controller. All of those setProperty()->setProperty()->setProperty() look incredibly fugly and I feel like I am doing it in the wrong place? Would it be a better option to just create some kind of service layer where I pass the entire $feed and then in that class I instantiate my Models and my Mapper?
Also, I need to normalize, which means I should be using a transaction, but I'm unsure where I should start my transaction. Because of the way I am doing things currently, the only place I could ever consider is in my Controller. wow.. that would be an awful place.
How can I get the model behaviour and operations out of my controller?
ImportController.php
public function indexAction() {
$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$url = "http://www.domain.com/admin/GetBookingData.aspx";
$client = new Zend_Http_Client();
$client->setParameterGet('dateEnteredMin', $start);
$client->setParameterGet('dateEnteredMax', $end);
$client->setParameterGet('login', 'login');
$client->setParameterGet('password', 'password');
$client->setUri( $url );
$client->setConfig(array(
'maxredirects' => 0,
'timeout' => 30));
// Send the request.
$response = $client->request();
// Grab the feed from ->getBody and add it to $feed
$feed = $this->csv_to_array(trim($response->getBody()));
// The first item in the array is the heading in the CSV, so we can remove it from the array using shift().
$title = array_shift($feed);
// Create my Models and Mappers.
// *** EVERYTHING BELOW HERE IS WHAT I DON'T LIKE ***
$bookings = new Bookings_Models_Bookings();
$property = new Bookings_Models_Property();
$clients = new Bookings_Models_Clients();
$bookingsMapper = new Bookings_Models_Bookings_Mapper();
$propertyMapper = new Bookings_Models_Property_Mapper();
$clientsMapper = new Bookings_Models_Clients_Mapper();
$bookings->setId($feed[9])
->setPropertyId($feed[1])
->setClientId($feed[2])
->setDate($feed[4]);
$bookingsMapper->save($bookings);
$property->setId($feed[1])
->setPropertyName($feed[23])
$propertyMapper->save($bookings);
$clients->setId($feed[2])
->setFirstName($feed[20])
->setLastName($feed[21])
$clientsMapper->save($clients);
}
Service layer is probably the way I'd go. So you'd create a service class that looks something like this:
class Your_Service_Import
{
public function importFromCsv($csv)
{
// etc.
}
}
you'd then move all of your controller method code that's after the csv_to_array call into that method, leaving the end of your controller method looking something like this:
$feed = $this->csv_to_array(trim($response->getBody()));
$service = new Your_Service_Import();
$service->importFromCsv($feed);
This makes it easier to test your import code (since it's in a standalone class) and easier to reuse in other parts of your application.
I'd go one step (or two steps) further than #Tim Fountain
Create a Service or Domain Helper that takes a start, end (and can be configured with a username password and url) and returns the csv list as an array.
Create a Service that maps a known dimension array (the csv) and maps it onto the database.
Your controller will then just be
$start = $this->getRequest()->getParam('start');
$end = $this->getRequest()->getParam('end');
$dataService = new Your_Service_Get();
$data = $dataService->get($start, $end);
$mapService = new Your_Service_Map();
$mapService->map($data);

Categories