Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 排行榜中显示的数据速度较慢_Php_Mysql_Symfony - Fatal编程技术网

Php 排行榜中显示的数据速度较慢

Php 排行榜中显示的数据速度较慢,php,mysql,symfony,Php,Mysql,Symfony,我在从数据库方案检索数据并将其显示在网格中时遇到一些问题,这是我使用的sql方案: 观点: {% extends 'MyBundle:Default:index.html.twig' %} {% block body %} <table class="table table-striped table-bordered"> <tr> <th>Player name</th>

我在从数据库方案检索数据并将其显示在网格中时遇到一些问题,这是我使用的sql方案:

观点:

{% extends 'MyBundle:Default:index.html.twig' %}
{% block body %}

    <table class="table table-striped table-bordered">
        <tr>
            <th>Player name</th>
            {% for action in actions %}
                <th>{{ action.displayName }}</th>
            {% endfor %}
        </tr>
        {% for stat in stats %}
            <tr>
                <td>{{ stat.playername }}</td>
                {% for key, action in actions %}

                    {% for key2, a in stat.actions|group('id', stat.id) if (key2 == (key + 1)) %}
                        <td>{{ a|length }}</td>
                    {% else %}
                        <td>0</td>
                    {% endfor %}

                {% endfor %}

            </tr>
        {% else %}
            <p>No statistics have been found</p>
        {% endfor %}
    </table>
{% endblock %}
一切正常,但加载速度很慢。我用虚拟记录填充了表格 我想这是因为我在玩家实体中建立了一个单一的关系,可以轻松地将所有东西分组。我只是不知道还有什么其他的可能性

这是玩家实体

use Doctrine\ORM\Mapping as ORM;

/**
 * Player
 *
 * @ORM\Table(name="player")
 * @ORM\Entity
 */
class Player
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="playerName", type="string", length=17)
     */
    private $playerName;

    /**
     * @var PlayerAction[]
     *
     * @ORM\OneToMany(targetEntity="PlayerAction", mappedBy="player")
     */
    private $actions;


    /**
     * Set id
     *
     * @param integer $id
     * @return Player
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set playerName
     *
     * @param string $playerName
     * @return Player
     */
    public function setPlayerName($playerName)
    {
        $this->playerName = $playerName;

        return $this;
    }

    /**
     * Get playerName
     *
     * @return string 
     */
    public function getPlayerName()
    {
        return $this->playerName;
    }

    /**
     * @param mixed $actions
     */
    public function setActions($actions)
    {
        $this->actions = $actions;
    }

    /**
     * @return mixed
     */
    public function getActions()
    {
        return $this->actions;
    }
}

一切正常意味着你的代码是正确的。可能需要检查表结构,可以运行以下命令:

mysqldump -d your_database_name 
并在此处共享输出。

我修复了它, 这是我正在执行的查询:

$queryActions = $em->createQuery(
    "SELECT a.actionName, a.id, a.displayName
    FROM MyBundle:Action a");

$actions = $queryActions->getResult();
$query = $em->createQuery(
    "SELECT p.playerName, a.actionName, SUM(a) as amount
     FROM MyBundle:Player p
     LEFT JOIN p.actions pa
     INNER JOIN pa.action a
     GROUP BY pa.action, p
     ORDER BY p.playerName ASC
     "
);

$result = $query->getResult();
这给了我一个可以使用的结果集,我只是对它进行了简化,这样它就可以给我一个很好的概述和很好的结果集来处理小树枝模板

$stats = array();
foreach($result as $record)
{
    $stats[$record["playerName"]][] = $record;
}


$uarray = array();

foreach($stats as $playerstat)
{
    # set all actions to 0 first, this way we prevent non sync stats
    foreach($actions as $action)
    {
        $uarray[$playerstat[0]['playerName']][$action['actionName']] = 0;
    }

    # now set it's good stats
    foreach($playerstat as $actionstat)
    {

        //$uarray[$actionstat['playerName']]["playerName"] = $actionstat['playerName'];

        $uarray[$actionstat['playerName']][$actionstat['actionName']] = $actionstat['amount'];
    }
}

$data['stats'] = $uarray;
$data['actions'] = $actions;

我希望这对某些人有用:

代码可以正常工作,只是需要花很多时间才能显示在屏幕上,我想用一种可能更好的方法来加速它,因为我的代码不太好,我想使用开发环境中的Symfony2调试工具栏来查看每个查询需要多少时间。
$stats = array();
foreach($result as $record)
{
    $stats[$record["playerName"]][] = $record;
}


$uarray = array();

foreach($stats as $playerstat)
{
    # set all actions to 0 first, this way we prevent non sync stats
    foreach($actions as $action)
    {
        $uarray[$playerstat[0]['playerName']][$action['actionName']] = 0;
    }

    # now set it's good stats
    foreach($playerstat as $actionstat)
    {

        //$uarray[$actionstat['playerName']]["playerName"] = $actionstat['playerName'];

        $uarray[$actionstat['playerName']][$actionstat['actionName']] = $actionstat['amount'];
    }
}

$data['stats'] = $uarray;
$data['actions'] = $actions;