Manipulate Doctrine NestedSet tree - zend-framework

I am using NestedSet behavior with doctrine 1.2.4 with Zend framework
but i am having some difficulty when inserting a child node of already saved root node
the Doctrine documentation showed the case of creating both root + child elements on the same page
while in my case , the root is already created and saved and i need to insert a child of it
here is an example
//// reading old order info
$order = new Order();
$orderInfo = $order->read($order_id);
$oldOrder = $orderInfo->toArray();
$oldOrder = $oldOrder[0];
//// building the new order information
$renew = new Orders();
$renew->domain_id = (int) $oldOrder["domain_id"];
$renew->auth_id = (int) $oldOrder["auth_id"];
$renew->price = $oldOrder["price"];
$renew->type = (string) $oldOrder["type"];
$renew->timestamp = $oldOrder["timestamp"];
$renew->save();
//// doctrine throwing an error here complaining the $orderInfo should be an instance of Doctrine_Record while its now an instance of Doctrine_Collection
$aa = $renew->getNode()->insertAsLastChildOf($orderInfo);
i don't really know how to retrieve the order from the db and how to convert it to doctrine_record or there is other ways to manipulate this nestedset
any suggestion would be appreciated

Try this:
// This will retrieve the 'parent' record
$orderInfo = Doctrine_Core::getTable('Order')->find($order_id);
// building the new order information
$renew = new Orders();
$renew->domain_id = (int) $oldOrder["domain_id"];
$renew->auth_id = (int) $oldOrder["auth_id"];
$renew->price = $oldOrder["price"];
$renew->type = (string) $oldOrder["type"];
$renew->timestamp = $oldOrder["timestamp"];
$renew->save();
$renew->getNode()->insertAsLastChildOf($orderInfo);
That should get a Doctrine Record of the parent node and you can use that to insert the child as the last child of.

Related

Entity Framework 6: is it possible to update specific object property without getting the whole object?

I have an object with several really large string properties. In addition, it has a simple timestamp property.
What I trying to achieve is to update only timestamp property without getting the whole huge object to the server.
Eventually, I would like to use EF and to do in the most performant way something equivalent to this:
update [...]
set [...] = [...]
where [...]
Using the following, you can update a single column:
var yourEntity = new YourEntity() { Id = id, DateProp = dateTime };
using (var db = new MyEfContextName())
{
db.YourEntities.Attach(yourEntity);
db.Entry(yourEntity).Property(x => x.DateProp).IsModified = true;
db.SaveChanges();
}
OK, I managed to handle this. The solution is the same as proposed by Seany84, with the only addition of disabling validation, in order to overcome issue with required fields. Basically, I had to add the following line just before 'SaveChanges():
db.Configuration.ValidateOnSaveEnabled = false;
So, the complete solution is:
var yourEntity = new YourEntity() { Id = id, DateProp = dateTime };
using (var db = new MyEfContextName())
{
db.YourEntities.Attach(yourEntity);
db.Entry(yourEntity).Property(x => x.DateProp).IsModified = true;
db.Configuration.ValidateOnSaveEnabled = false;
db.SaveChanges();
}

Zendframework 2 postgresql update with "not"

Is is possible to pass to database the following sql query using tableGateway, if so, how would such a command look like ?
UPDATE table_data SET active = not active where table_data.id = 12;
You need to use a Zend\Db\Sql\Expression, this class tells Zend\Db that you know what you're doing and that the content of the string passed to this class shouldn't be transformed, but rather used as is.
// build the table gateway object
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
$tableIdentifier = new TableIdentifier('table_data', 'public');
$tableGateway = new TableGateway($tableIdentifier, $adapter);
// create the filter
$where = new \Zend\Db\Sql\Where();
$where->equalTo('id', '12');
// update table
$tableGateway->update(
['active' => new \Zend\Db\Sql\Expression('not active')],
$where
);

how to set form element value in zend framework for editing

I am using zend framework. I have done user registration and login but i can't do update user information because i don't know how to show form element value in update form.Please tell me any solution.
$editClientForm = new Form_AddNewClientForm();
$editClientForm->setAction($this->view->baseUrl().'/client/edit');
//fetch client data from your database through model
$clientInfo = $mdlClient->fetchClientDetails($id);
//populate the data to form
$editClientForm->populate($clientInfo);
$this->view->editClientForm = $editClientForm;
Update
After taking suggestion of #php-dev:
$editClientForm = new Form_AddNewClientForm();
$editClientForm->setAction($this->view->baseUrl().'/client/edit');
//fetch client data from your database through model
$mdlClient = new Model_Client();
$clientInfo = $mdlClient->fetchClientDetails($id);
//populate the data to form
$editClientForm->populate($clientInfo);
$this->view->editClientForm = $editClientForm;
Inside your model:
public function fetchClientDetails($id)
{
$row = $this->_db->fetchRow("SELECT * from tablename where id = $id");
return $row;
}

Entity framework performing an Insert, when it should be doing an Update

I am having a real issue with the EF v1. I have quite a big EDMX with maybe 50 entities mapped, but this one entity is causing me grief.
The entity has mappings to other entities which in effect are reference tables, but for some reason it is trying to do an insert and not just update itself.
Here is a fragment of my code:
using (var context = new someEntities()) {
var studentCourseJoin =
context.StudentCourseJoinSet.Where(o => o.Code == scjCode).First();
studentCourseJoin.EntryStatus = new EntryStatus { Code = viewModel.StudentDetails.EntryStatusCode };
studentCourseJoin.ParentalInHigherEducation = new ParentalInHigherEducation { Code = viewModel.StudentDetails.ParentalInHigherEducationCode };
studentCourseJoin.School = new School { Code = viewModel.StudentDetails.SchoolCode };
studentCourseJoin.Institution = new Institution { Code = viewModel.StudentDetails.InstitutionCode };
studentCourseJoin.LastSchoolEndYear = viewModel.StudentDetails.LastSchoolEndYear;
studentCourseJoin.LastInstitutionEndYear = viewModel.StudentDetails.LastInstitutionEndYear;
// Blows up here trying to do an insert on the studentCourseJoin.Institution.
// But if I removed this one, then it will blow up on another one.
context.SaveChanges(true);
}
If anyone has ANY ideas please, they would help a lot.
Try adding those lines before calling SaveChanges:
ObjectStateEntry entry = context.ObjectStateManager.GetObjectStateEntry(studentCourseJoin);
entry.ChangeState(EntityState.Modified);
Update:
Try this for Institution instead:
studentCourseJoin.Institution = context.Institutions.FirstOrDefault(i => i.Code == viewModel.StudentDetails.InstitutionCode);

Yet again Entity Framework and FK problems

I have an entity with two fk's. I've been trying to insert a record to the database without success. This are the approaches I've used:
valuePaymentBetToAdd.BetType = db.BetTypes.First(betType => betType.Id == valuePaymentBetToAdd.BetType.Id);
valuePaymentBetToAdd.Lottery = db.Lotteries.First(lotto => lotto.Id == valuePaymentBetToAdd.Lottery.Id);
In this case the second object gets assigned but when calling the SaveChanges method I get an error saying that the properties of the Lottery object were null.
valuePaymentBetToAdd.BetTypeReference.EntityKey = new EntityKey(db.DefaultContainerName + ".BetType", "Id", valuePaymentBetToAdd.BetType.Id);
valuePaymentBetToAdd.LotteryReference.EntityKey = new EntityKey(db.DefaultContainerName + ".Lottery", "Id", valuePaymentBetToAdd.Lottery.Id);
In this case I get another weird error. When the object is being added to the collection.
The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object.
Am I missing something in this case?
Try setting the EntityReference like this:
valuePaymentBetToAdd.BetTypeReference.EntityKey = b.BetTypes.First(betType => betType.Id == valuePaymentBetToAdd.BetType.Id).EntityKey;
It works for me
How about creating a stub object for BetType and Lottery where you set only the Id property, and then attach those to their respective EntitySets, and then setting these objects on you Bet object, and save - something like:
Lottery lottery = new Lottery() { Id = valuePaymentBetToAdd.Lottery.Id };
BetType betType = new BetType() { Id = valuePaymentBetToAdd.BetType.Id };
MyContext.AttachTo("Lottery", lottery);
MyContext.AttachTo("BetType", betType);
valuePaymentBetToAdd.Lottery = lottery;
valuePaymentBetToAdd.BetType = betType;
MyContext.AddToBet(valuePaymentBetToAdd);
MyContext.SaveChanges();