Unable to access set within set | C++ - c++

I have a class named "Store", it has 2 data fields of type "set": "customers" (of type std::set) and "albums" (of type std::set<Album>).
std::set<Album> albums;
std::set<Customer> customers;
Now, when I try to save data to one of those sets - everything works fine , but in the type "Customer" I have a third set named orders ( of type std::set ), and when I try to save data to it (and after I leave the function that saves the data) it disappears!
here is how I save the data to it:
Customer foundCustomer = *Customer::findID(customers ,id);
Order newOrder = Order();
newOrder.ordNum = ordNum;
newOrder.isPaid = false;
foundCustomer.orders.insert(newOrder);
tell me if you need me to post more code.
Help would be much appreciated !

Customer foundCustomer = *Customer::findID(customers ,id);
This line makes a copy of the customer. If you want to modify the original customer int he set, you would need to use a reference:
Customer& foundCustomer = *Customer::findID(customers ,id);
And make sure that *Customer::findID(...) results in a reference as well.

Related

Use a queryset to display two filtered data in django?

I have a table Documents and it has a field named type and the type has two data's namely - type1 & type2
Now my requirements is - i have a bootstrap tabs type1 & type2, and i need to display data accordingly on the template
Which is efficient way to do this ?
Using two variables
data = Documents.objects.filter(id=pk)
type1 = data.filter(type="type1")
type2 = data.filter(type="type2")
and then pass it to context
context = { "type1":type1,"type2":type2 }
Is there any other best way to do this ?
Seems like they really have to be different querysets since they are different sets of data to be rendered. Also assuming id is your primary key (which looks like it is), note that the usage of Documents.objects.filter(id=pk).filter(type=...) would just consider that 1 object with that pk, you might want to change it to Documents.objects.filter(type=...). Then you can make it a bit more dynamic by listing the possible types and then passing the filtered data to the template where the key is the type.
doc_types = ("type1", "type2")
context = {
doc_type: Documents.objects.filter(type=doc_type)
for doc_type in doc_types
}

Doctrine returning strange field names from query

I am using "doctrine/doctrine-orm-module": "^2.1" (it is a module for zend framework 3). I want to create a query which will return rows with field names (trivial, right?). But instead of exact names of fields I am getting this query result:
SELECT
u0_.id AS id_0, u0_.username AS username_1, u0_.email AS email_2,
u0_.first_name AS first_name_3, u0_.last_name AS last_name_4,
u0_.password AS password_5, u0_.status AS status_6, u0_.created AS created_7,
u0_.modified AS modified_8
FROM
user_item u0_
ORDER BY
u0_.id DESC
This query is generated by this code:
$entityManager = $this->getEntityManager();
$queryBuilder = $entityManager->createQueryBuilder();
$queryBuilder->select('u')
->from(UserItem::class, 'u')
->orderBy('u.id', 'DESC')
;
$query = $queryBuilder->getQuery();
echo $query->getSql();
print_r($query->getParameters());
die('|||');
What is the "0_" appending to the table name? What is appendings "_x" to the fields name?
How can I get normal fields and tables names without appended "_x"?
Just names, I'm assuming both the first_name and last_name as shown in that generated SQL, right?
I changed the order below, makes it easier to read / understand.
What you want to do is (pseudo code): Select from UserItem all the first & last names
So, write the code that way :)
$queryBuilder
->from(UserItem::class, 'u')
->select(['u.first_name', 'u.last_name'])
->orderBy('u.id', 'DESC'); // Might want to sort by either u.first_name or u.last_name
What's in the QueryBuilder?
->from(UserItem::class, 'u') - First parameter is the FQCN (Fully Qualified Class Name) of the Entity you wish to use with the QueryBuilder. Not required is the second parameter, which is an alias to use for this instance of the QueryBuilder to recognize the FQCN defined class by. (Off of the top of my head it defaults to snake_case'd names of the class, in this case "user_item")
->select(['u.first_name', 'u.last_name']) - Function takes a "mixed" param. Click through to its definition and you'll see the following in the function:
$selects = is_array($select) ? $select : func_get_args();
Which indicates that it will always pass the "$selects" on the next bit as an array. (Another hint is that $selects is plural)
->orderBy('u.id', 'DESC') - Creates a rule to order results by. If you click through to this function, you'll see that this one ends like so:
return $this->add('orderBy', $orderBy);
Meaning: you can add more than 1 order by.
When it comes to the generated DQL:
u0_ is the table alias as defined in the DQL, from your question: FROM user_item u0_, this will later be transformed to MySQL (usually) which will be the same. It sets u0_ as an alias for user_item.
The _* appended to property names is just plain the order of the columns as they've been created in the database (have a look, they'll be in that order).
Lastly, the fact you were receiving entire entities and not just the names (first_name & last_name) is due to ->select('u'). Because no property (or properties as shown above) is defined, Doctrine assumes you wish to receive the whole enchalada. Doing ->select('u.first_name') would then get you just the first names, and using an array as above would get you more than 1 property.
Hope that helped you out :)

Removing non unique Objects from a List (identifiable by a property)

I am capturing events from a stream, each event is a Device Object. The way the stream work is that it is on a timer, so it picks up the same device multiple times and adds to the stream.
I am putting all theres is a List<Device> and putting that into another stream.
I have create a StreamTransformer in the attempt to remove duplicate from the list and then add the unique list back into the stream.
This transform code below, I have tried to add to set and back to list, but this hasn't worked I assume due to the fact they are objects not strings.
//Transform Stream List by removing duplicate objects
final deviceList = StreamTransformer<List<Device>, List<Device>>.fromHandlers(
handleData: (list, sink) {
List<Device> distinctList = list.toSet().toList();
sink.add(distinctList);
});
I have attempted to use .where and other libraries but to no avail and am hoping for some guidance.
Device Object contains unique id and name that could be used to filter
out duplicates
Question: How can I remove duplicate objects from a List in Dart?
Thanks in advance.
First of all you would need to define by which criteria the objects are supposed to be unique. Is there an identifying property for example? If that is the case the following options should work.
The most efficient way is probably to make the approach with a set working. For that you would need to turn your objects into data objects, meaning have them identify for equality by property values. For that you would override the equality operator and hashCode getter. However this changes how your objects behave on every equality operation, so you would have to judge if that is suitable. See this article.
Another option is to just filter manually using a Map:
class MyObj {
String val;
MyObj(this.val);
}
TestListFiltering(){
List<MyObj> l = [
MyObj("a"),
MyObj("a"),
MyObj("b"),
];
// filter list l for duplicate values of MyObj.val property
Map<String, MyObj> mp = {};
for (var item in l) {
mp[item.val] = item;
}
var filteredList = mp.values.toList();
}

How do you correctly return an aggregate data field using AX 2012 Query Service

I have been working on the AX Query Service as of late. I have a pretty good understanding of everything but it seems that the QueryDataFieldMetadata object does not like aggregates. When I build a QueryDataFieldMetadata object:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Database;
And add it to the data source everything is fine. But when I do this:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Sum;
And add it to the data source the field is not returned at all in the results set. I have checked the datasource itself before executing the query and it is in the fields list but nothing is returned. Does anyone know why this might be happening? Any help would be appreciated.
I just figured this one out. The problem was due to me selecting another field from the table but forgetting to put it in the "Group by" fields. It is strange to me that the query service was returning THAT field with an empty but not returning the aggregate fields at all. Basically I had made a query service query that would be equal to this:
Select wMSLocationId, SUM(AvailPhysical), RecId from InventSum group by ItemId, InventLocationId, wMSlocationId where ItemId == 'some value';
The query was returning:
InventSum.wMSLocationId = 001
InventSum.RecId = 0
The inclusion of the RecId was a mistake, I had forgotten to remove it, but didn't think it would matter as it wasn't in the group by fields and would therefore return null. Removing this selection field did result in the aggregate field returning in the query.
Anyway I hope this helps someone out there as it took me some time to figure out.

How to convert entity leaving it id

There are some entities (Region, Country, City) which used STI (or even CTI). How it possible convert Country to City leaving old id?
This is not supported in Doctrine 2 because type casting user objects is not supported by PHP.
With that said, Doctrine uses the discriminator column to determine what type of object to hydrate. If you change the value of the discriminator column in the database with a SQL UPDATE, the object type will be changed the next time the object is loaded. This works for STI, but CTI would be more complicated.
It may not be possible by standard using Doctrine, but you can work around it.
If you use the Class Metadata you can select your discriminator column.
Take a look at the Trait that I've created to solve the problem within my app:
namespace App\Doctrine\Repository;
trait DiscriminatorTrait
{
abstract public function getClassMetadata();
abstract public function getEntityManager();
private function updateDiscriminatorColumn($id, $class)
{
$classMetadata = $this->getClassMetadata();
if (!in_array($class, $classMetadata->discriminatorMap)) {
throw new \Exception("invalid discriminator class: " . $class);
}
$identifier = $classMetadata->fieldMappings[$classMetadata->identifier[0]]["columnName"];
$column = $classMetadata->discriminatorColumn["fieldName"];
$value = array_search($class, $classMetadata->discriminatorMap);
$connection = $this->getEntityManager()->getConnection();
$connection->update(
$classMetadata->table["name"],
[$column => $value],
[$identifier => $id]
);
}
}
I do have to warn you though, when your sub-classes have (a lot of) extra fields you will need to fill or clear them manually afterwards.