Using ZF and Doctrine. I have a table 'pages'. I want to have it so that some pages can have sub pages associated with it. The sub pages should also have a reference to their (one and only) parent page.
I know I could split this between 2 entities (page / subpage), but I know it's possible with one entity, but can't figure out how to make the relationship work.
The way I invision it, is a second table would be the mapping table (page_id, parent_page_id).
I'm using annotation reference in my entities and here is what i'm doing so far, any help would be appreciated, in a late night crunch.
/**
* #OneToMany(targetEntity="Page", mappedBy="parentPage")
*/
private $subPages;
/**
* #ManyToOne(targetEntity="Page", inversedBy="subPages")
*/
private $parentPage;
The answer for this question is here, in the Doctrine documentation, under One-To-Many, Self-referencing:
https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-self-referencing
Here is an example:
class Pages{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* one page have several referred pages
* #var Collection
* #ORM\OneToMany(targetEntity="Pages", mappedBy="parentPage")
*/
private $subPages;
/**
* many referred pages have one referral page
* #ORM\ManyToOne(targetEntity="Pages", inversedBy="subPages")
* #ORM\JoinColumn(name="parent_page", referencedColumnName="id")
*/
private $parentPage;
private $referredBy;
public function __construct()
{
$this->subPages = new Doctrine\Common\Collections\ArrayCollection();
}
}
Related
I use Swagger UI to display API documentation. By default, it displays the "Models" section at the bottom:
How to hide it?
To hide the "Models" section, add defaultModelsExpandDepth: -1 to the Swagger UI configuration code in your index.html.
Note the option name uses plural Model*s* not Model.
// index.html
<script>
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
defaultModelsExpandDepth: -1, // <-------
Swagger UI also has many other configuration options that control API documentation rendering.
For .Net Core 3.0 just Add c.DefaultModelsExpandDepth(-1); on your Startup.cs
// Startup.cs
app.UseSwaggerUI(c =>
{
c.DefaultModelsExpandDepth(-1); // Disable swagger schemas at bottom
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Your API V1");
});
In Docket bean just add
new
Docket(DocumentationType.SWAGGER_2).ignoredParameterTypes(YourClass.class,YourAnother.class)
I hope its helpful
If using Django add this inside your settings.py:
SWAGGER_SETTINGS = {
'DEFAULT_MODEL_DEPTH':-1
}
Although not the exact results you are looking for but i find it perfect to just disable the properties of the model you don't want via:
<?php
// src/AppBundle/Entity/User.php
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
...
* #ApiResource(
* attributes={
* "normalization_context"={"groups"={"api"}},
* "denormalization_context"={"groups"={"api"}}
* },
...
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #Groups({"api","user:read"})
*/
protected $id;
/**
* #var \DateTime
*/
private $disabledProperty;
This way you will get a model just with the props you exposed via the group api.
Hope this helps someone :)
I want to make a Language chooser in Laravel 5.1, but i know how I can make it, but I want that it remembers the selected language (so if I visit the page again, that I have still the samen language). But how can I do it? And have I need to store it in the DB?
Better explained here:
So if a visitor joins the site for the first time, then the language will be "English" and then he can choose his language that he/she want's. If the same person leaves and joins at another time, then the language would be the same as the person selected earlier.
I’ve written a blog post about this, see Detect and change language on the fly with Laravel for details but basically you need a Middleware to attribute a default locale:
/**
*
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Session::has('locale')) {
$locale = Session::get('locale', Config::get('app.locale'));
} else {
$locale = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
if ($locale != 'fr' && $locale != 'en') {
$locale = 'en';
}
}
App::setLocale($locale);
return $next($request);
}
And a method (along with a form) to store a language change:
/**
* Change session locale
* #param Request $request
* #return Response
*/
public function changeLocale(Request $request)
{
$this->validate($request, ['locale' => 'required|in:fr,en']);
\Session::put('locale', $request->locale);
return redirect()->back();
}
I have a user table with a store id column (store) that correspondent with a store table.
I'm retrieving this store id with a custom entity object.
Application/Entity/User.php
namespace Thuiswinkelen\Entity;
class User extends \ZfcUser\Entity\User
{
/**
* #var int
*/
protected $store;
public function getStore()
{
return $this->store;
}
/**
* Set store.
*
* #param int $store
* #return UserInterface
*/
public function setStore($store)
{
$this->store = (int) $store;
return $this;
}
}
My question is: How to get the store name in the store table (with an inner join?)
It would be great when I can use something like:
<?php echo $this->zfcUserStoreId() ?>
<?php echo $this->zfcUserStoreName() ?>
If you're looking to map entity relationships; you will need an object relational mapper (ORM), such as Doctrine to accomplish this.
This will convert your foreign identifiers into objects in which you can then transverse.
$storeName = $user->getStore()->getName();
I'm guessing that the examples you have suggested $this->zfcUserStoreId() you have invented due to the already existing ZfcUser\View\Helper\ZfcUserDisplayName
This however is a view helper and simplify aids the rendering of a user's name based on other configuration.
I'm using primefaces 3.5, with a glassfish server 3.1.2. I have a trivia question game that relies on the user to select answers. I have two tables which one is generated based on if it's a multi-select question, or if its mutlipile choice. While my multiple choice data table works beautifully, the other does not. I've followed the example on the show case, and when I select 2 of the table, and hit the next button on the wizard it is in, it deselects what I selected and keeps me on the same page. I made it stay on the same page on any exception, and the exception was a null pointer due to the fact that the "selected answers" where null. Here is my table.
<p:dataTable
id="multiQuestionTable"
value="#{triviaQuestionsBean.dataModel}"
var="answer"
selection="#{triviaQuestionsBean.selectAnswers}">
<p:column selectionMode="multiple" />
<p:column>
#{answer.answer.testAnswer}
</p:column>
</p:dataTable>
The setting and getter:
private QuestionAnswers[] selectAnswers;
public QuestionAnswers[] getSelectAnswers() {
return selectAnswers;
}
public void setSelectAnswers(QuestionAnswers[] selectAnswers) {
this.selectAnswers = selectAnswers;
}
The setter is never called, but the data model that is used works very well for the single select. If that is needed to figure out my issue let me know. Please assist if possible.
public class QuestionAnswersDataModel extends ListDataModel
implements SelectableDataModel {
/**
* This is the question answers data model used to allow for the sorting,
* and selection of items in a JSF dataTable. This is the basic no-arg
* constructor --Important-- This judges the data from the id, so if the ID
* has not been assigned, there will be unpredictable results.
*
*/
public QuestionAnswersDataModel() {
}
/**
* This is the question answers data model used to allow for the sorting,
* and selection of items in a JSF dataTable. This is the constructor where
* the list of elements are instantiated. --Important-- This judges the data
* from the id, so if the ID has not been assigned, there will be
* unpredictable results.
*
* #param data The list of QuestionAnswers to display in the table.
*/
public QuestionAnswersDataModel(List<QuestionAnswers> data) {
super(data);
}
/**
* This takes a "row key" and looks through the wrapped data to find the
* specific QuestionAnswers entity that matches the passed in row key
*
* #param rowKey The key to search with
* #return The QuestionAnswers entity that matches the criteria or null if
* nothing matches
*/
#Override
public QuestionAnswers getRowData(String rowKey) {
/**
* Get the wrapped data (If there was a lot of data you would use a
* query not just a list)
*/
List<QuestionAnswers> answers =
(List<QuestionAnswers>) getWrappedData();
//for each answer
for (QuestionAnswers answer : answers) {
//if the answer's unique identifier matches the row key:
if (answer.getQuestionAnswersId().toString().equals(rowKey)) {
//return it
return answer;
}
}
//if nothing matches return null
return null;
}
/**
* This takes a QuestionAnswers entity object and returns a key for the
* identification of this entity. As this one runs off of the ID of the
* answer, if nothing is assigned to the value, a null key will be returned.
*
* #param answer The answer to generate the key of
* #return The identifier for this object or null if the ID is null
*/
#Override
public Object getRowKey(QuestionAnswers answer) {
//if the answer is null, return null
if (answer == null) {
return null;
}
//else get the answer id
Long id = answer.getQuestionAnswersId();
//if it's null return null
if (id == null) {
return id;
}
//else return the String representation of the id
return id.toString();
}
I am trying to return a list of all members, and also include their membership number if they have it.
The models are setup as listed below:
The member class:
class Member {
/**
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #Column(type="string", length=255) */
protected $email = '';
}
The membership class:
class Membership {
/** #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #OneToOne(targetEntity="Member") */
protected $member = 0;
/** #Column(type="integer") */
protected $membership_number = 0;
}
I want to create a query that returns ALL the members, AND also shows a membership number if it exists for that member.
Can't figure out how to do this.
The query below selects all members with a membership, not what I need though.
Right joins don't work so I'm not sure what to do.
$this->_em->createQuery("SELECT m, mb FROM Membership mb JOIN mb.member m");